Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing requests that end in ".cshtml" to a controller

(This is cross-posted to the ASP.NET forms)

I'm working on the WebGit .NET project, and we are close to a "1.0" release. However, I'm having trouble getting my "Browse" controller (which pulls files out of the repository) to serve-up ".cshtml" files.

I originally had trouble with ".config" and ".cs" files as well, but I fixed that with this in the web.config:

  <location path="browse">
    <system.webServer>
      <security>
        <requestFiltering>
          <fileExtensions allowUnlisted="true">
            <clear />
          </fileExtensions>
          <hiddenSegments>
            <clear />
          </hiddenSegments>
        </requestFiltering>
      </security>
    </system.webServer>
  </location>

The routing that should be handling this request (that is successfully routing everything else) is:

routes.MapRoute(
    "View Blob",
    "browse/{repo}/blob/{object}/{*path}",
    new { controller = "Browse", action = "ViewBlob", path = UrlParameter.Optional });

Now, whenever I try to access a URL that ends in ".cshtml", it gives a 404, even though my request should have been handled by the "Browse" controller. The files I'm serving-up do not exist on disk, but are instead pulled from a git repository as blobs. Every other file extension that I have tried works just fine.

How can I fix this behavior?


EDIT: I have tried disabling WebPages like so:
<appSettings>
  <add key="webpages:Enabled" value="false" />
</appSettings>

But that appears to have no effect.

like image 268
John Gietzen Avatar asked Dec 28 '22 15:12

John Gietzen


2 Answers

As a quick workaround, you can put a temporary browse.cshtml file at your application root and put this inside your web.config, add key="webpages:Enabled" value="false"

like image 52
imran_ku07 Avatar answered Dec 31 '22 02:12

imran_ku07


This is a known bug in ASP.NET WebPages, which gets implicitly loaded when you are using MVC 3. I don't think there is a straightforward way of disabling this behavior. The only workaround is to use a different extension (specifically, one that is not listed via WebPageHttpHandler.GetRegisteredExtensions())

This will be fixed in MVC 4, however. Sorry for the inconvenience.

like image 44
marcind Avatar answered Dec 31 '22 03:12

marcind