Parsing Razor files without their extensions and hosting providers

Topics: ASP.NET 2.0
Jul 31, 2011 at 9:20 AM

My current hosting provider says that .cshtml files should be referenced without their extensions for them to work in their environment: eg. /admin/Settings/Themes instead of /admin/Settings/Themes.cshtml

pages open in this particular hosting environment, however I don't know how to get BlogEngine to serve links without the .cshtml extension. Does BlogEngine support this, or should I tell my hosting company that what they want is unrealistic?

Thanks,

Radi A.

Coordinator
Jul 31, 2011 at 1:24 PM

The reason why that's not very practical is because in at least one case, (the admin\Extensions) folder, there is 5 CSHTML files in there.  They cannot all be default documents.  The only way to make that work would be to create 5 subfolders, putting a CSHTML into each one of them and renaming each of those CSHTML files to a default document name such as default.cshtml.  And then the BE code would need to be changed so it is aware of these new URLs.

So yes, the hosting company is unrealistic.  The CSHTML extension is no different than any other extension -- ASPX, HTML, ASP, PHP, etc.  Nobody requires these other file types to be put into folders by themselves and renamed to be a default document.  So there's no reason why it should be required for CSHTML files to be setup this way.

Jul 31, 2011 at 1:40 PM
Edited Jul 31, 2011 at 1:44 PM

Hey Ben,

I think I wasn't clear on one point. In /admin/Extensions/ there's a few .cshtml files (editor.cshtml, setstatus.cshtml and so on). I can access them with ~/admin/extensions/editor and ~/admin/extensions/setstatus, but not ~/admin/extensions/editor.cshtml. (This is somehow a setting on the hosting infrastructure.)

I personally feel it should be configurable on the hosting side, the folder/file paradigm is still valid, however MVC tends to use views, not pages. This link http://msdn.microsoft.com/en-us/magazine/hh148144.aspx describes the ASP.NET Web Pages should handle URLs with and without the extension (check Preserving Site URLs), and it would be great if BlogEngine supports that.

(NOTE: I updated the file names to match real ones)

Thanks,

Radi A.

Coordinator
Jul 31, 2011 at 2:13 PM

Thanks for the additional info.

Do you mean at that host, if you pull up /admin/extensions/editor, it pulls up the editor.cshtml page, and not a 404 error?  If that works, then I imagine (but could be wrong) that they have some type of URL rewriting setup to map /editor to editor.cshtml.  It's a little odd though since as I mentioned, CSHTML is a normal extension and normally you should be able to directly access a CSHTML file.

If the host has the URL Rewrite Module available (it's a module for IIS7), you could try adding a rewrite rule so .CSHTML is rewritten so the .CSHTML part is dropped off.  Not sure how this would work in that environment.

One of the feature requests in the Issue Tracker is to have "a highly configurable" rewrite system.  Most people want to drop the typical .ASPX extension, or they want to change the URL for posts so they are not /post/something.aspx, but are maybe instead just something.aspx (without the /post/ part), etc.  This would be a different reason why you would want a "highly configurable" rewrite system ... to workaround the .CSHTML extension not being directly accessible.

Jul 31, 2011 at 2:59 PM

so if I pull up /admin/settings/themes the Themes page opens correctly. /admin/settings/themes.cshtml gives me a server error (it doesn't serve .cshtml files). I can set the MIME TYPE but I don't know what to put as Content, I tried with "application/xml" and it tries to serve the file as XML, and the browser fails rendering it. I haven't tried with all .cshtml files, but /admin/extensions/editor and default fail.

I actually tried this on a normal Webmatrix and ASP.NET 4 project (not BlogEngine), every .cshtml can be accessed without the .cshtml extension. I have yet to figure out how that actually works, but reading about MVC I'm getting to think that it is normal. I don't think the hosting provider has a URL Rewrite Module.

Aug 2, 2011 at 1:46 AM

I had the same issue with the admin pages (cshtml) in the sub/virtual blogs not working.. worked fine in the primary blog though.. I 'think' the problem is with the url rewriter, but I found the easiest thing to do is to add a wildcard mapping in iis (i'm using iis6). to do this, simply goto the properties of the website, and on the 'Home Directory' tab, select the configuration for the Application Settings. On the bottom of the 'Mappings' Tab, then a wildcard application map by selecting 'insert' and pointing to the aspnet_isapi.dll in you .net 4 install (usually C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll). Also untick the option 'Verify that file exists', click OK, then restart your site. Things worked fine for me after that.

As i said though, I'm using IIS6, not sure on the specific configurations of iis7 in terms of this, so you may need to have a quick look around