C# “不提供此类页面。” 尝试浏览 *.cshtml 文件时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12934932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"This type of page is not served." error when trying to browse on *.cshtml files
提问by Michael Schnerring
I just create a new MVC 4 Web API project, and create a new .cshtml file, containing very simple HTML:
我只是创建了一个新的 MVC 4 Web API 项目,并创建了一个新的 .cshtml 文件,其中包含非常简单的 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:
打开 URL 时显示以下错误:
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
“/”应用程序中的服务器错误。
不提供此类页面。
描述:您请求的页面类型未被提供,因为它已被明确禁止。扩展名“.cshtml”可能不正确。请检查下面的 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 whereto search? Neither local IIS nor Visual Studio Development Server works.
我在一位同事的开发机器上测试了这个场景(完全相同的项目)。它按预期工作。所以我想一定是配置或某些安装有问题。但是在哪里搜索呢?本地 IIS 和 Visual Studio 开发服务器都不起作用。
UPDATE
更新
Accessing a .cshtml file directly isn't for production code - it's for training purposes, only!
Navigating to /Home/Indexworks perfectly fine. So there is nothing else, which is obviously wrong. Just accessing .cshtml files directly.
直接访问 .cshtml 文件不适用于生产代码 -仅用于培训目的!导航到/Home/Index工作得很好。所以没有别的,这显然是错误的。直接访问 .cshtml 文件。
采纳答案by Darin Dimitrov
UPDATE 2:
更新 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 ~/Viewsfolder.
我终于明白你想要达到的目标了。对不起,我最初不理解。我没有仔细阅读你的问题。您正在尝试直接访问~/Views文件夹外的 Razor 页面。
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:
在 ASP.NET MVC 4 中这是默认禁用的。为了启用它,您所要做的就是在您的 web.config 中调整以下设置:
<add key="webpages:Enabled" value="true" />
It's value is falseby 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.
false当您使用任何模板创建新的 ASP.NET MVC 4 项目时,它的默认值是。因此,如果您说它可以在他的 PC 上运行,我猜您的同事已经这样做了。
ORIGINAL
原来的
You should not request directly a .cshtmlfile in an ASP.NET MVC application. Those are views located in the ~/Viewsfolder. 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.cshtmlview with the contents shown in your question.
Now when you run your application you could navigate to /Home/Indexwhich will execute the Indexaction of the Homecontroller and render the corresponding view.
I would recommend you reading some getting started tutorials about ASP.NET MVCin order to familiarize yourself with the basic most fundamental concepts.
UPDATE 1:
The code that blocks requests to .cshtmlfiles inside the ~/Viewsfolder is situated inside the ~/Views/web.configfile:
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
不应.cshtml在 ASP.NET MVC 应用程序中直接请求文件。这些是位于~/Views文件夹中的视图。它们不能直接访问。您需要相应的控制器操作。
例如,假设您有以下控制器:
public class HomeController: Controller
{
public ActionResult Index()
{
return View();
}
}
然后~/Views/Home/Index.cshtml使用问题中显示的内容定义视图。
现在,当您运行您的应用程序时,您可以导航到/Home/Index哪个将执行控制器的Index操作Home并呈现相应的视图。
我建议您阅读一些有关ASP.NET MVC 的入门教程,以熟悉最基本的基本概念。
更新1:
阻止对.cshtml文件~/Views夹内~/Views/web.config文件的请求的代码位于文件内:
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
回答by Ryan Zakariudakis
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
如果您已启用网页并且您已验证 .NET 版本有效,那么您的问题很可能是 IIS 服务器没有可在所有站点之间共享的 razor 二进制文件。要修复它,您应该确保已将 mvc 二进制文件包含在 /bin 文件夹中。这个链接很好地解释了它:http:
//www.codeproject.com/Answers/437259/his-type-of-page-is-not-served-CSHTML-how-to-serve#answer1
回答by cdonahue
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.
我遇到了同样的问题,我通过转到客户端的属性并在显示“开始操作”的地方选择当前页面来解决它。我已将其设置为特定页面。
回答by DickoVan
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:
我在 MVC 项目中收到了相同的消息,结果发现我错过了 MVC 框架的一个关键部分,即 Global.asax 和 Global.asax.cs 条目。我在解决方案资源管理器中选择了该项目,然后添加、新建项目、全局应用程序类。然后我编辑它并用必要的配置项替换 Application_Start :
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
This solved it for me.
这为我解决了它。

