简体   繁体   English

如何通过.aspx访问.cshtml页面

[英]How to access .cshtml page via .aspx

Yes, this is a newbie question and might be ridiculous, but I'm apparently having tremendous difficulty finding an answer to get up and running. 是的,这是一个新手问题,可能很荒谬,但是我显然很难找到一个启动和运行的答案。

I've created an ASP.NET Razor (v3) web site using the Visual Studio template. 我已经使用Visual Studio模板创建了一个ASP.NET Razor(v3)网站。 That's fine and I can see everything locally. 很好,我可以在本地看到所有内容。 However, when I try to view the Default.cshtml page on the server I get this error: 但是,当我尝试在服务器上查看Default.cshtml页面时,出现此错误:

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. 扩展名“ .cshtml”可能不正确。 Please review the URL below and make sure that it is spelled correctly. 请查看下面的URL,并确保其拼写正确。

Requested URL: /4.6.1Razor/Default.cshtml 要求的网址:/4.6.1Razor/Default.cshtml

My server administrator says this: You are not supposed to access a cshtml file directly on a production server, it's blocked by default. 我的服务器管理员这样说:您不应该直接在生产服务器上访问cshtml文件,默认情况下该文件处于阻止状态。 Please access an html, htm, asp or aspx file and use that to get the data from the cshtml. 请访问html,htm,asp或aspx文件,并使用该文件从cshtml获取数据。

Okay ... so how exactly do I access a cshtml file via an aspx file? 好的...那么我如何通过aspx文件访问cshtml文件呢? Or can someone point me to a working sample that I can look at? 或者有人可以指出我可以查看的工作样本?

The default files that Visual Studio created did not include an html, htm, asp, or aspx file so I'm a little confused as to how to start that. Visual Studio创建的默认文件不包含html,htm,asp或aspx文件,因此对于如何启动它,我有些困惑。 Also, the samples that Visual Studio points to are of course broken: http://www.asp.net/aspnet/samples/aspnet-web-pages . 而且,Visual Studio指向的示例当然是坏的: http : //www.asp.net/aspnet/samples/aspnet-web-pages

A little help pointing me to the right direction would be appreciated. 向我指出正确方向的一点帮助将不胜感激。 Thanks. 谢谢。

Maybe the question should be ... What kind of page / file (.asp or .aspx or what) do I need to render a .cshtml file and how do I do that. 也许问题应该是...我需要呈现哪种类型的页面/文件(.asp或.aspx或什么),以及如何呈现。 I've tried a .aspx file with the code: @RenderPage("Default.cshtml") but when I view the file in a browser it just echoes @RenderPage("Default.cshtml") instead of rendering anything. 我已经尝试使用以下代码编写.aspx文件:@RenderPage(“ Default.cshtml”),但是当我在浏览器中查看该文件时,它仅回显@RenderPage(“ Default.cshtml”)而不呈现任何内容。

This answer suggests you need to enable WebPages in the <appSettings> section of web.config : 此答案表明您需要在web.config<appSettings>部分中启用WebPages:

<add key="webpages:Enabled" value="true" />

If this is not enough, check this question for server software requirements to process WebPages. 如果这还不够,请检查此问题以了解处理WebPage的服务器软件要求。

For IIS application configuration, have a look at this description . 对于IIS应用程序配置,请查看此描述

If that doesn't work. 如果那不起作用。 Go to IIS --> MIME Types --> Check if the extension .cshtml exists, if not add it. 转到IIS-> MIME类型->检查扩展名.cshtml是否存在,如果没有添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM