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?

tereško
  • 58,060
  • 25
  • 98
  • 150
mythz
  • 141,670
  • 29
  • 246
  • 390

2 Answers2

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" />
davidfowl
  • 37,120
  • 7
  • 93
  • 103
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! :-)

Community
  • 1
  • 1
Danny Tuppeny
  • 40,147
  • 24
  • 151
  • 275
  • Unfortunately System.Web.Mvc.ViewEngines requires a dependency to MVC - I really want to remove all deps and existing behaviour. – mythz Jun 25 '11 at 22:10
  • Ah, I see. Can you remove cshtml from the [`RazorCodeLanguages.Languages` property](http://msdn.microsoft.com/en-us/library/system.web.razor.razorcodelanguage.languages%28v=vs.99%29.aspx)? – Danny Tuppeny Jun 26 '11 at 10:03
  • Nope, it still tries to run but I get this exception now: http://pastebin.com/SPvLfn8G - It looks like WebPageHttpModule is the issue, do you know how I could remove it? – mythz Jun 26 '11 at 10:50
  • 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): http://msdn.microsoft.com/en-us/library/ms164632.aspx – Danny Tuppeny Jun 26 '11 at 10:53
  • Yeah doesn't work either (has no effect) - I think it's because they're being dynamically registered using the new PreApplicationStartMethod http://goo.gl/ysNb0 – mythz Jun 26 '11 at 11:04
  • 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 http://twitter.com/#!/DanTup/status/84943521408229376 – Danny Tuppeny Jun 26 '11 at 11:19
  • Hey cool thx, BTW I've created a new question with just trying to remove the WebPageHttpModule at: http://goo.gl/Hv8Vc – mythz Jun 26 '11 at 11:41