Disable Razors default .cshtml handler in a ASP.NET Web Application

我只是一个虾纸丫 提交于 2019-12-24 02:37:06

问题


Does anyone know how to disable the .cshtml extension completely from an ASP.NET Web Application?

In essence I want to hijack the .cshtml extension and provide my own implementation based on a RazorEngine host, although when I try to access the page.cshtml directly it appears to be running under an existing WebPages razor host that I'm trying to disable.

Note: it looks like its executing .cshtml pages under the System.Web.WebPages.Razor context as the Microsoft.Data Database is initialized. I don't even have any Mvc or WebPages dlls referenced, just System.Web.dll and a local copy of System.Web.Razor with RazorEngine.dll

I've created a new ASP.NET Web .NET 4.0 Application and have tried to clear all buildProviders and handlers as seen below:

<system.web>
    <httpModules>
        <clear/>
    </httpModules>
    <compilation debug="true" targetFramework="4.0">
        <buildProviders>
            <clear/>
        </buildProviders>
    </compilation>

    <httpHandlers>
        <clear/>
        <add path="*" type="MyHandler" verb="*"/>
    </httpHandlers>
</system.web>

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
        <clear/>
    </modules>
    <handlers>
        <clear/>
        <add path="*" name="MyHandler" type="MyHandler" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
    </handlers>
</system.webServer>

Although even with this, when I visit any page.cshtml page it still bypasses My wildcard handler and tries to execute the page itself.

Basically I want to remove all traces of .cshtml handlers/buildProviders/preprocessing so I can serve the .cshtml pages myself, anyone know how I can do this?


回答1:


If you're trying to turn off ASP.NET webpages, you can set this flag in app settings:

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



回答2:


You should be able to register your own custom ViewEngine in the Application_Start method. Scott Hanselman blogged a sample that uses a custom ViewEngine for mobile devices, but the ideas should be the same for what you're trying to do.

Edit (again): David Fowler suggests:

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

I always wondered what that setting was for, but never got around to investigating! :-)



来源:https://stackoverflow.com/questions/6479986/disable-razors-default-cshtml-handler-in-a-asp-net-web-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!