"此类型的页不提供早餐"错误,当试图浏览上* .cshtml文件 [英] "This type of page is not served." error when trying to browse on *.cshtml files

查看:635
本文介绍了"此类型的页不提供早餐"错误,当试图浏览上* .cshtml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚创建一个新的MVC 4个Web API项目,并创建一个新的.cshtml文件,其中包含非常简单的HTML:

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>

在打开的URL显示以下错误:

When opening the URL the following error displays:

中的服务器错误'/'应用。

Server Error in '/' Application.

这类型的页不提供早餐。

This type of page is not served.

说明:您请求不提供服务,因为页面的类型
  它已被明确禁止。扩展名.cshtml可能
  不正确。请检查下面的URL并确保它是
  拼写正确。

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.

请求的URL:/index.cshtml

Requested URL: /index.cshtml

我我的一位同事的显影机上测试这种情况下(完全相同的项目)。它的工作预期。所以我想一定有什么错误的配置或一些安装。但其中,以搜索?无论是本地IIS也不Visual Studio开发服务器的工作。

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.

更新

访问.cshtml文件并不能直接生产code - 这是用于训练目的,
导航到 /首页/指数工作完全正常。因此,有没有别的,这显然是错误的。只需直接访问.cshtml文件。

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.

推荐答案

更新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.

在ASP.NET MVC 4这是默认情况下禁用。为了使这一切,你需要做的就是调整以下设置,在你的web.config:

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" />

它的值是默认情况下,当您创建使用任何的模板创建一个新的ASP.NET MVC 4项目。所以我想你的同事已经这样做,如果你说,它工作在PC上。

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.

<击>
原始

您不应该在ASP.NET MVC应用程序直接请求 .cshtml 文件。这些都是位于〜/浏览次数文件夹视图。它们是不能直接访问。你需要一个相应的控制器动作。

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();
    }
}

,然后定义了〜/查看/主页/ Index.cshtml 在你的问题中所示的内容视图。

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.

我建议你阅读有关 ASP.NET MVC 一些入门教程,以便与基本的最基本的概念,熟悉

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

更新1:

在code,阻止请求 .cshtml 文件中的里面的〜/浏览次数文件夹内位于在〜/查看/ web.config中文件:

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>

这篇关于&QUOT;此类型的页不提供早餐&QUOT;错误,当试图浏览上* .cshtml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆