如何在不使用MVC的情况下从浏览器直接使用.cshtml扩展名调用.cshtml文件 [英] How to call .cshtml file directly with .cshtml extension from browser without using MVC

查看:188
本文介绍了如何在不使用MVC的情况下从浏览器直接使用.cshtml扩展名调用.cshtml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在IIS 8中调用.cshtml页面时,出现以下错误

I'm getting the below error when i used to call the .cshtml page in IIS 8

"/"应用程序中的服务器错误. 无法提供此类型的页面. 说明:由于明确禁止了您请求的页面类型,因此无法提供.扩展名".cshtml"可能不正确.请查看下面的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.

请求的URL:/_header.cshtml

Requested URL: /_header.cshtml

推荐答案

要提供CSHTML文件以定向浏览器请求,您需要在web.config中将以下appsetting设置为true.默认情况下,在web.config中此值设置为false.

To serve CSHTML files to direct browser requests, you need set following appsetting in web.config to true. By default this value is set to false in web.config.

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

有关此设置的更多信息,请阅读此资源.

For more informat about this setting, Read this resource.

我的视图"文件夹中的Web.config是-

Web.config inside my Views Folder is -

<configuration>
  <!--<configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>-->

  <!--<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization"/>
        <add namespace="System.Web.Routing" />
        <add namespace="WebApplication1" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>-->

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

  <system.webServer>
    <handlers>
       <!--<remove name="BlockViewHandler"/>
           <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />-->
    </handlers>
  </system.webServer>
</configuration>

通过此配置,我能够使CSHTML进入浏览器并显示内容.

With this config, I was able to get the CSHTML hit the browser and contents get displayed.

我不建议您进行上述设置,因为不建议通过RAZOR View Engine获取视图.而是将所有静态文件放在一个文件夹中,然后将这些异常添加到http管道中.

I wouldn't suggest you to do the above settings as by passing RAZOR view Engine for views is not advisable. Instead put all the static files in a folder and add those exceptions to the http pipeline.

这篇关于如何在不使用MVC的情况下从浏览器直接使用.cshtml扩展名调用.cshtml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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