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

2

3

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?

Miguelmiguela answered 25/6, 2011 at 19:20 Comment(0)
R
3

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

<add key="webpages:Enabled" value="false" />
Restate answered 27/6, 2011 at 1:15 Comment(0)
A
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! :-)

Akvavit answered 25/6, 2011 at 20:12 Comment(7)
Unfortunately System.Web.Mvc.ViewEngines requires a dependency to MVC - I really want to remove all deps and existing behaviour.Miguelmiguela
Ah, I see. Can you remove cshtml from the RazorCodeLanguages.Languages property?Akvavit
Nope, it still tries to run but I get this exception now: pastebin.com/SPvLfn8G - It looks like WebPageHttpModule is the issue, do you know how I could remove it?Miguelmiguela
You should be able to clear that in the same was as you did the httpHandlers in your web.config sample (see edit in answer): msdn.microsoft.com/en-us/library/ms164632.aspxAkvavit
Yeah doesn't work either (has no effect) - I think it's because they're being dynamically registered using the new PreApplicationStartMethod goo.gl/ysNb0Miguelmiguela
Hmmm, fail :-( Then I'm out of ideas. I'm curious though, so I did ask on Twitter though, in case any of the MS ASP.NET guys might be able to help twitter.com/#!/DanTup/status/84943521408229376Akvavit
Hey cool thx, BTW I've created a new question with just trying to remove the WebPageHttpModule at: goo.gl/Hv8VcMiguelmiguela

© 2022 - 2024 — McMap. All rights reserved.