Disable Razors default .cshtml handler in a ASP.NE

2019-07-17 12:26发布

问题:

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! :-)