How To Render HTML Views Asp.Net MVC

Asp.Net by default serves views with .cshtml extension, that makes it possible to use various Asp.net inbuilt functions and html helpers to used with it. But sometime the facilities to make coding easier sucks.

Situation 1

I have setup a project to use Asp.net MVC/Web API using C# and using AngularJs for binding views at front-end. Now as I get the data from my controllers using $http of angularjs I want to ibnd it on my view using angular, and to get this done I create pure HTML view in Views Folder of MVC project. And when I compile… Oops!!

Situation Something Else

I may have some static files in my view folders and I really don’t want Asp.Net to compile those files for me, so I get it as .html extension, these are pure html content files now. And I go ahead and compile the project.. Oops again!

Reason

Asp.Net doesn’t serve pure html views from Views folder by default. We may have to tell compiler that we want to use pure html files from Views folder, once we ask it will do it for us.

Solution

How To Render HTML Views Asp.Net MVC
How To Render HTML Views Asp.Net MVC

.

We can add handler for the required extension to be served under System.Webserver tag in Web.config under Views  folder.

 <!-- web.config under the Views folder -->

<system.webserver>
<handlers>
<!-- handler for serving html view in mvc -->
<add name="HtmlScriptHandler" path="*.html" verb="*" precondition="integratedMode" type="System.Web.StaticFileHandler" />
</handlers>
</system.webserver>

.

If you don’t want to set a handler and just want to render the html view you can use File/ FilePathResult method:

 //return the file using FilePathResult method
 return new FilePathResult("path_to_file.html", "text/html");

 //return the file using File method
 return File("path_to_file.html", "text/html");

Say something : I accept all the "Humer&Critic"