C# and Razor - The type of page you have requested

2020-02-28 03:30发布

I have looked through other posts but none seem to answer what I need.

  • I created an empty site in WebMatrix (ASP.NET)
  • I opened that site in VWD 2013
  • I hit F5 and it runs fine on a URL such as http://local.com:59833/ContentPage.cshtml
  • I go to http://local.com/cscsu_bi/ContentPage.cshtml and it doesn't work with the error below

    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: /cscsu_bi/ContentPage.cshtml

  • The web.config file is as follows

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
    <add key="webpages:Enabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
</configuration>

I am on Windows 7. Is there anything obvious I'm doing wrong?

Thanks

标签: c# razor
6条回答
劳资没心,怎么记你
2楼-- · 2020-02-28 03:41

In my case the culprit was the fact that Global.asax was placed under the '/Views' folder. This was somehow working while the project was targeting MVC3. Once the solution got upgraded to MVC4, running said solution would result in the aforementioned errors. In a similar vein, other errors would crop up mentioning:

"no default webpage has been set and directory browsing has been disabled"

The solution: After upgrading to MVC4+ I had to manually move Global.asax under the root folder of the container project. Be sure to inspect Global.asax to make sure that the namespace applied within is the proper one. Just my 2c.

查看更多
叛逆
3楼-- · 2020-02-28 03:44

I have set up Razor on a number of machines, and often find that I need to include the following DLLs in my bin folder:

  • System.Web.Razor.dll
  • System.Web.WebPages.Deployment.dll
  • System.Web.WebPages.dll
  • System.Web.WebPages.Razor.dll
  • Microsoft.Web.Infrastructure.dll
查看更多
Lonely孤独者°
4楼-- · 2020-02-28 03:51

This may not be your exact issue, but I have had this happen for a couple reasons:

  1. Incorrect Framework on Server: Make sure the target framework of the project is supported by the server. Just changing the targetFramework attribute of the compilation element may not be enough as the DLLs for your project may be compiled against the 4.5 framework.

  2. Incorrect Framework of Application Pool: This can also happen if your application pool is not using the right .Net CLR version. Make sure it is using the 4.0 instead of 2.0

查看更多
贪生不怕死
5楼-- · 2020-02-28 03:52

Take a look at this article.

http://www.asp.net/web-pages/overview/more-resources/aspnet-web-pages-(razor)-troubleshooting-guide

This fixed my issue after reviewing all IIS settings and references.

Make sure that the root of your website has at least one .cshtml file in it.

查看更多
甜甜的少女心
6楼-- · 2020-02-28 03:53

In my case I set webpages:Enabled to true under appSettings:

<appSettings>
  <add key="webpages:Enabled" value="false" />
</appSettings>   

According to post: what is the function of webpages:Enabled in MVC 3 web.config, this prevents files in the Views folder from being directly accessible from a web browser.

查看更多
Bombasti
7楼-- · 2020-02-28 04:01

I was missing the Microsoft.AspNet.WebPages NuGet package. Installing it solved my problem. (This package has dependencies on Microsoft.Web.Infrastructure and Microsoft.Web.Razor and thus includes them for you)

If you use the NuGet console, just type:

Install-Package Microsoft.AspNet.WebPages
查看更多
登录 后发表回答