Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"This type of page is not served." error when trying to browse on *.cshtml files

Tags:

c#

asp.net-mvc

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.

like image 261
Michael Schnerring Avatar asked Oct 17 '12 13:10

Michael Schnerring


People also ask

How do I read a Cshtml file?

Right click the Index. cshtml file and select View in Browser. You can also right click the Index. cshtml file and select View in Page Inspector.

What is Cshtml file format?

A CSHTML file is a C# HTML webpage file used by Razor, an ASP.NET view engine that generates webpages. It is similar to a standard ASP.NET webpage (. ASP or . ASPX file) but uses slightly different syntax. CSHTML files run on a web server, which generates HTML for a client web browser.


1 Answers

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> 

like image 130
Darin Dimitrov Avatar answered Sep 19 '22 07:09

Darin Dimitrov