Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 323
alex.mironov Avatar asked Jul 30 '13 14:07

alex.mironov


People also ask

How can add HTML file in MVC project?

Step 1: Right click on the "Controllers" folder and add "LoadHtml" controller. Copy and paste the following code. Step 2: Right click on the "Index" action method in the "LoadHtmlController" and add "Index" view.

HOW include HTML file in asp net?

net controls or masterpage as you said, just include the file you already have. Show activity on this post. Instead of creating separate html file, you can create User Control. If you are trying to create separate html you need to specify all the tags for e.g. <html><head></head><body></body></html> etc.

What are static files in MVC?

Host static files in ASP.NET MVC ASP.NET MVC supports placing static files side by side with files that should be kept private on the server. IIS and ASP.NET require explicitly restricting certain files or file extensions from being served from the folder in which an ASP.NET app is hosted.

How do I serve a static file in NET Core?

Serve static filesStatic files are stored within the project's web root directory. The default directory is {content root}/wwwroot , but it can be changed with the UseWebRoot method. For more information, see Content root and Web root. The preceding code was created with the web app template.


2 Answers

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 
like image 195
Darin Dimitrov Avatar answered Oct 29 '22 08:10

Darin Dimitrov


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> 

like image 32
cs3x Avatar answered Oct 29 '22 10:10

cs3x