41

I just create a new MVC 4 Web API project, and create a new .cshtml file, containing very simple HTML:

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title></title>
</head>
<body>
    <div>

    </div>
</body>
</html>

When opening the URL the following error displays:

Server Error in '/' Application.

This type of page is not served.

Description: The type of page you have requested is not served because it has been explicitly forbidden. The extension '.cshtml' may be incorrect. Please review the URL below and make sure that it is spelled correctly.

Requested URL: /index.cshtml

I tested this scenario (exactly the same project) on a developing machine of one of my colleagues. It worked as expected. So I guess there must be something wrong with the configuration or some installation. But where to search? Neither local IIS nor Visual Studio Development Server works.

UPDATE

Accessing a .cshtml file directly isn't for production code - it's for training purposes, only! Navigating to /Home/Index works perfectly fine. So there is nothing else, which is obviously wrong. Just accessing .cshtml files directly.

SteveC
  • 15,808
  • 23
  • 102
  • 173
Michael Schnerring
  • 3,584
  • 4
  • 23
  • 53

4 Answers4

61

UPDATE 2:

I have finally understood what you are trying to achieve. Sorry for me not understanding initially. I didn't read your question carefully enough. You are trying to directly access a Razor page outside of the ~/Views folder.

In ASP.NET MVC 4 this is disabled by default. In order to enable it all you have to do is adjust the following setting in your web.config:

<add key="webpages:Enabled" value="true" />

It's value is false by default when you create a new ASP.NET MVC 4 project using any of the templates. So I guess your colleague already did this if you are saying that it works on his PC.

ORIGINAL

You should not request directly a .cshtml file in an ASP.NET MVC application. Those are views located in the ~/Views folder. They are not directly accessible. You need a corresponding controller action.

For example let's say that you have the following controller:

public class HomeController: Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

and then have defined the ~/Views/Home/Index.cshtml view with the contents shown in your question.

Now when you run your application you could navigate to /Home/Index which will execute the Index action of the Home controller and render the corresponding view.

I would recommend you reading some getting started tutorials about ASP.NET MVC in order to familiarize yourself with the basic most fundamental concepts.


UPDATE 1:

The code that blocks requests to .cshtml files inside the ~/Views folder is situated inside the ~/Views/web.config file:

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
        <remove name="BlockViewHandler"/>
        <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
</system.webServer>

SteveC
  • 15,808
  • 23
  • 102
  • 173
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • True statement, but it doesn't really address the question. It's like watching the presidential debates! – Mikeb Oct 17 '12 at 13:04
  • 2
    @Mikeb, how does this not address the question? – Mike Perrenoud Oct 17 '12 at 13:05
  • 1
    OP wants to know where the configuration information is in the server that is creating this error that he observes on one machine and not the other. Yes, he *shouldn't* serve cshtml, but on one machine he can (or is) and on the other one he can't - so what is causing that difference? – Mikeb Oct 17 '12 at 13:11
  • 1
    @Mikeb - The best answers are the ones that address the question OP _should_ be asking but doesn't know to... In other words, addressing the fundamental misconception behind the question is often more useful than addressing the question directly. – James Curtis Oct 17 '12 at 13:18
  • I would note that the OP specifically says this is in a Web API project, not an MVC project. Your answer is still valid -- I just thought people arriving from elsewhere should be aware of the difference. – Heretic Monkey Oct 17 '12 at 13:23
  • 3
    Thanks for this very detailed answer. But in this case I didn't want to follow any best practices. Currently I watch a video series at Pluralsight, which explains how to set up a *Single Page Application*. I'm pretty new to MVC, so I really test a lot with little test projects. And this just didn't work for me. I'd just like to test my code as shown in the video and I'd like to locate and solve this issue - mainly for interest. Sorry for not mentioning this in my post earlier. – Michael Schnerring Oct 17 '12 at 14:10
  • @ebeeb, the code I provided is located in the `~/Views/web.config` file and you are showing me the `~/web.config` file in your updated answer which is not the same thing. – Darin Dimitrov Oct 17 '12 at 14:23
  • I fixed my post. Sorry, I didn't know that there is another Web.config. But I still think that this doesn't cause/solve my issue. Removing this code doesn't work, either. And actually my colleague successfully ran the solution with containing these "blockers". – Michael Schnerring Oct 17 '12 at 14:28
  • @darin-dimitrov Thanks for this. I had a BlogEngine.NET installation under a MVC root application and the BlogEngine.NET wouldn't serve the cshtml pages until I added the webpages:Enabled key. Thanks for keeping this updated. – raddevus Nov 29 '16 at 16:05
1

If you have enabled Web Pages and you have verified that the .NET version is valid then your problem is most likely that the IIS server does not have the razor binaries available to be shared across all sites. To fix it you should ensure you've included the mvc binaries in your /bin folder. This link explains it very well:
http://www.codeproject.com/Answers/437259/his-type-of-page-is-not-served-CSHTML-how-to-serve#answer1

1

I had the same issue and I resolved it by going to the properties of the Client and where it says "Start Action", select current page. I had it set to specific page.

cdonahue
  • 11
  • 1
  • A question should not be posted as an answer. You can ask a new question by clicking the [Ask Question](http://stackoverflow.com/questions/ask) button and refer to this post. – Anthony May 23 '17 at 23:15
  • Sorry.. this was an answer as to how I handled this error. it isn't a question. – cdonahue May 26 '17 at 15:14
  • Hi, I'm sorry I don't know why I pasted my "not an answer" comment here. Obviously, this is an answer. – Anthony May 26 '17 at 15:52
-1

I had the same message in an MVC project and it turned out that I was missing a critical piece of the MVC framework, the Global.asax and Global.asax.cs entries. I selected the project in Solution Explorer and did an Add, New Item, Global Application Class. I then edited it and replaced the Application_Start with the necessary config items:

            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
            }

This solved it for me.

DickoVan
  • 59
  • 5