I want to add file extension to razor view engine other than cshtml. Any clue you guys?
-
Doing a service that returns a text based file. – Idrees Dec 12 '11 at 15:28
-
I'm new to MVC3 myself, but it seems like that would be some kind of resource, rather than the view itself? Maybe that should be your question. – Paul Bellora Dec 12 '11 at 15:31
-
related: http://stackoverflow.com/questions/7074002/display-contents-of-text-file-in-mvc3-razor – Paul Bellora Dec 12 '11 at 15:32
-
@KublaiKhan in that case you aren't using Razor which is what the OP asked : ) – Adam Tuliper Dec 12 '11 at 15:37
-
The file extension doesn't make any difference; there's no point in changing it. – SLaks Dec 12 '11 at 15:43
-
@SLaks is it doable? Can we add to the extension list that the razor view engine is expecting. Can we add to (aspx, asax, cshtml, and vbhtml)? – Idrees Dec 12 '11 at 16:56
-
Yes, [it is](http://stackoverflow.com/questions/5110970/can-i-serve-html-files-using-razor-as-if-they-were-cshtml-files-without-changi), but there is no point. – SLaks Dec 12 '11 at 17:05
-
@SLaks the team leader dropped the idea. So COOL :D – Idrees Dec 13 '11 at 09:17
3 Answers
Since you are looking at your own view type here, use the virtual path provider. http://rebuildall.umbraworks.net/2009/11/17/ASP_NET_MVC_and_virtual_views

- 29,982
- 4
- 53
- 71
I've made a MVC service that generates CSS files based on configuration files.
I'm returning the CSS file using the Razor view. Simply the view has CSS syntax and placeholders. The place holders are replaced with values from the passed model.
I wanted the razor view engine to view .css file besides .cshtml, just for the sake of intellisense support in the visual studio.
I believe the solution to this problem starts here
You can use *.cshtml view files to return other file types in the web app (they don't have to contain html). For example, you can use attribute-based routing to return a URL with any file extension (info here: ASP.NET MVC path with file extension), and within the *.cshtml you can set the content-type, eg:
@{
Response.ContentType = "text/plain";
}
The main disadvantage of doing is this is that your VisualStudio intellisense will be mucked up for the markup portion of the file - but other than that it works pretty well.