How do you request static .html files in the ~ / Views folder in ASP.NET MVC?

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

But when I request a file, a 404 error occurs.

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

What did I miss?

+69
asp.net-mvc asp.net-mvc-routing
Jul 30 '13 at 14:12
source share
4 answers

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

You can not. In this folder there is a web.config file, which explicitly prohibits access to it from any file. If you want to have access to files from the client, these files should not be placed in the Views folder, which is of particular importance in ASP.NET MVC.

You may have a ~/Static folder where you can place your HTML files. And then enter it like this:

 http://example.com/yourapplicationname/static/foo.html 
+83
Jul 30 '13 at 14:13
source share

To allow files such as js and html in the Views folder, edit the web.config file in the view 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> 

+58
Sep 06 '14 at 5:24
source share

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

Good. The noted answer is not entirely correct, although it gives a solution.

The arguments given in the correct answer are correct: in the "Views" folder, which prohibits access to files directly, web.config is set (BlockViewHandler parameter). It is designed to protect views in Asp.Net MVC. But if you asked a question about servicing these files directly, then you probably have good reason for this, for example, to use partial views of AngularJS (as in our case), where we do not want to duplicate the views folder with strange names.

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

Change it

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

- at -

 <add name="BlockViewHandler" path="*.cshtml" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" /> 
+29
Jul 16 '14 at 19:27
source share

Another alternative is to insert an action method into any of the required controllers in order to serve the html file

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

Access html as http: // yoursite / controller / MyHtml . You can extend this action method to take the html file name as the method / querystrign parameter and display the file at run time, for example, something like this.

  public ActionResult MyHtml(string htmlPageName) { var result = new FilePathResult($"~/Views/{htmlPageName}.html", "text/html"); return result; } 
+9
Oct 13 '16 at 7:58
source share



All Articles