Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

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

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.

question from:https://stackoverflow.com/questions/12934932/this-type-of-page-is-not-served-error-when-trying-to-browse-on-cshtml-files

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...