95

I want to be able to request static .html files which are located in the ~/Views folder. According to the documentation, the routing system checks to see if a URL matches a disk file before evaluating the application's routes.

But when I request the file a 404 error arises.

My file is located in ~/Views folder. The URL is: http://[localhost]/Views/HtmlPage1.html

What have I missed?

6 Answers 6

114

I want to be able to request static .html files which are located in the '~/Views' folder.

You can't. There's a web.config file in this folder which explicitly forbids accessing any file from it. If you want to be able to access files from the client those files should not be placed in the Views folder which has a special meaning in ASP.NET MVC.

You could have a ~/Static folder where you could place your HTML files. And then access it like that:

http://example.com/yourapplicationname/static/foo.html
3
  • 5
    Or, instead of naming your folder 'static' you could mimic the controller name structure that is found inside the Views folder.. so the URL has the same 'appearance'. E.g. since you can't put your plainjane Edit.html in Views/Order/Edit.html - you put it in ~/Order/Edit.html
    – bkwdesign
    Feb 20, 2015 at 13:58
  • 2
    This has now changed with the lastest ASP.NET (vNext) where you can register static provider for html files but also still route the normal MVC way. Only had a quick play with it but that is what I did with a test project. Running through kestrel on Linux.. but IIS should be the same I suppose.
    – Piotr Kula
    Mar 21, 2015 at 11:02
  • 1
    @bkwdesign I am sure its configuration controlled by mimicking the folder structure messed up my controllers default handler and made it unaccessible. So test this situation out. Apr 6, 2015 at 17:17
79

To allow files like js and html in Views folder edit the web.config in views-Folder:

<system.webServer>
<handlers>
  <add name="JavaScriptHandler" path="*.js" verb="*"
     preCondition="integratedMode" type="System.Web.StaticFileHandler" />      
  <add name="HtmlScriptHandler" path="*.html" verb="*"
     preCondition="integratedMode" type="System.Web.StaticFileHandler" />
  <remove name="BlockViewHandler"/>
  <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>

4
  • 1
    I had to remove the line "routes.RouteExistingFiles = true;" in RouteConfig.RegisterRoutes for this to work
    – Sam Sippe
    Sep 25, 2015 at 2:00
  • 1
    While the marked answer contains useful information, this is the better answer. Tested and working in my current project.
    – A. Murray
    Apr 17, 2017 at 9:21
  • 1
    What about css files? Sep 29, 2017 at 17:18
  • @Darin Dimitrov's answer gives a workaround, but this should be marked as the answer
    – Kunal
    Jul 12, 2018 at 21:54
31

I want to be able to request static .html files which are located in the ~/Views folder.

Well you can. The marked answer is not entirely correct, although it gives a solution.

The reasoning in the marked answer is correct, it is web.config (BlockViewHandler setting to be specific) in the Views folder that prevents the files to be accessed directly. It is there for securing the views in Asp.Net MVC. But if you asked a question about serving these files directly then you likely have a valid reason to do so, like using AngularJS partial views (as in our case) where we do not want to duplicate the views folder with weird names.

So here is a very simple tweak you can do in the web.config file found in the Views folder, without compromising security of your asp.net mvc views. This will secure the .cshtml files as usual but leave your .html files alone.A

Change this

<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode"    type="System.Web.HttpNotFoundHandler" />

--to--

<add name="BlockViewHandler" path="*.cshtml" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
2
  • 3
    This could still be dangerous with other files like .aspx or .ascx files or those from third-party view engines. Much better to whitelist as per cs3x's answer.
    – pwdst
    Dec 4, 2014 at 15:36
  • 1
    @pwdst: If you're using a mixture of server side views then yes whitelisting individual types might be better. We just want to protect the .cshtml files so we used this as it is a bit less verbose.
    – Vikas
    Dec 30, 2014 at 16:58
15

Another alternate option is to insert an action method in any of the desired controller to serve the html file

public ActionResult MyHtml()
{
    var result = new FilePathResult("~/Views/HtmlPage1.html", "text/html");
    return result;
}

Access the html as http://yoursite/controller/MyHtml. You may extend this action method to accept the html file name as method/querystrign parameter and render the file at run time, e.g something like this.

 public ActionResult MyHtml(string htmlPageName)
 {
      var result = new FilePathResult($"~/Views/{htmlPageName}.html", "text/html");
      return result;
 }
1

If you are planning to use inside view folder, above answers should be best but this answer may be useful for users who are migrating to asp.net mvc core. Placing files in wwwroot instead of views folder should make your html pages access easily as localhost/myfile.html

0

You can place it in the /Content folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.