user2927139
user2927139

Reputation: 21

How to call .cshtml file directly with .cshtml extension from browser without using MVC

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

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

Upvotes: 2

Views: 3735

Answers (3)

ramiramilu
ramiramilu

Reputation: 17182

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

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

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.

Upvotes: 4

Britton
Britton

Reputation: 2981

To use a cshtml page without MVC you have to use what Microsoft calls "ASP.NET Web Pages." It is another technology inside ASP.NET like Web Forms and MVC. You should be able to enable it by adding this to your web.config.

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

Upvotes: 3

Craig W.
Craig W.

Reputation: 18155

In the Views folder of your application you will find a web.config. In that web.config you will find the following entry.

<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />

Basically that entry says that no matter what HTTP verb someone uses it should refuse to return anything that is in the Views folder or a subfolder.

You could remove that entry, but that means someone could request your raw views which could be potentially dangerous.

If you don't want to run the view through Razor then you should create a plain HTML file. You can either put that HTML file in a different folder in your application or you can change the BlockViewHandler to be more specific.

<add name="BlockViewHandler" path="*.cshtml" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />

Upvotes: 0

Related Questions