inutan
inutan

Reputation: 10888

Error - This type of page is not served

I have an MVC (version 5.0) application that I am running from VS 2012 with Web setting of 'Use Visual Studio Development Server'.
When the active file in VS is non .cshtml file, routing works absolutely fine. However, if the active file is a .cshtml file and I run the application in debug mode, I get this error -

Server Error in '/' Application.
This type of page is not served. 

FYI, snippet from RegisterRoutes() -

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Employee", action = "Index", id = UrlParameter.Optional }
    ); 

What makes loading of application behave differently when .cshtml file is active/in focus in Visual Studio?

Upvotes: 0

Views: 590

Answers (1)

fdomn-m
fdomn-m

Reputation: 28621

MVC is built on top of asp.net, so uses the same mechanics for serving files. Because .cshtml files are views and should be served via a controller (MVC) and not served directly, they are blocked as you have found.

You should set the url to the view as defined in the controller (eg /Employee/Index) rather than the page that is used to generate the view (eg /Views/Employee/Index.cshtml)

You can do this by changing the project settings:

  • right click project
  • properties
  • Web
  • Start URL (or Specific Page)

(it's probably set to 'Current Page')

If, for some reason, you do want to see the source code of the view, then it's blocked via the machine's web.config with this line:

<add path="*.cshtml" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>

I recommend you don't change this.

Upvotes: 2

Related Questions