How to access HTML files from ASP.NET MVC VIEWS Folder - asp.net-mvc

I will like add conventional HTML page under VIEWS folder (in ASp.NET MVC) page.
I have added the route exceptions as mentioned below.
routes.IgnoreRoute("{resource}.htm/{*pathInfo}")
routes.IgnoreRoute("{resource}.html/{*pathInfo}")
Although it does work when I put the html files out of VIEWS folder but I get Page not found 404 when I put those in VIEWS folder. I am also unable to browse the VIEWS folder by setting directory browsing option in IIS.
Please help me on HOW to access HTML file from VIEWS folder.

I think that it's a mistake to mix your HTML content with your views. I'd suggest that you create a separate static folder under Content and put your HTML there. You can create an analogous directory structure to your view structure if necessary for management. Then you don't need to do anything special in order to able to reference the files. You can even, then, open them up to editing with Contribute, etc. by people who are allowed to modify static content.
+-Content
+-Images
+-Static
+-Account
+-privacy.html
+-refunds.html
+-Styles
Usage:
<a href='<%= Url.Content( "~/Content/Static/Account/privacy.html" ) %>'>Privacy Policy</a>

The default Views folder has an Web.config file that explicitly gives 404 errors for all requests. You just need to edit and enable for HTML files (or all files, but then people might snoop).

Related

MVC .NET Core 2.0 - _Layout static files do not load for action methods of a controller

I started a new project in VS 2017 and created a ASP.NET Core 2.0 web application (Model View Controller). Then I've changed the conent of the _Layout.cshtml to the interface I want to use (included #Renderbody etc.) and included all the static content it requires in the wwwroot and save and Ctrl+F5 and the layout shows with all the correct formatting and functionality. No problem so far:
However from this point forward none of the static content files of the _Layout shows in any other view. So for example if I visit any of the following URLS:
http://localhost:52786/home/
http://localhost:52786/home/index
http://localhost:52786/home/about
http://localhost:52786/home/contact
I see this:
Startup.cs already has app.UseStaticFiles() in Configure()
So it sounds like you may have your HTML body content in the wrong place. With ASP.NET MVC &ASP.NET Core MVC, the HTML for each page is served up from the /Views directory (by the Home controller by default), not from the /wwwroot, and it is C# HTML (.cshtml) just like the _Layout.cshtml file. Things like images, static javascript etc. are what's typically located in the /wwwroot directory as these are the parts of your website that are client side instead of server-side.
Try replicating your index, about and contact HTML content as .cshtml files under the /Views/Home directory, replacing whatever's there in the template, except for #{ViewData["Title"] = "Home";} located at the top. The layout template should then serve these as the body content (where #renderbody... is in _Layout.cshtml).
If this still doesn't work I'll need a bit more info about what you've changed from the default template. Hope this helps!
Thank you James for your response. I figured it out and it's actually a noob mistake which I'm posting so that other new developers don't get caught in it.
When you want to reference the content in the wwwroot folder if you reference them without "~/" before the name of the directory it works for the _Layout page which fooled me to think it should work for other parts of the application but you should include "~/" before the folder names explicitly otherwise the static files will be un reachable.

Password protect with Identity an independent HTML project

So we've got a project that our tutorial software exports as an HTML project. (Root has an index.html file, folders for js, css, etc)
Our support portal with Identity account management runs on ASP.NET MVC.
What I'd like is for the HTML project to be accessible, but you must login first.
I've gone about it two ways, unsuccessful both times:
I can put the HTML project in a static folder in the root of the ASP project. This way I can access it as domain.com/Test/Course/index.html. Everything works fine here, js and css are loaded properly, but it is not password protected.
I can put it in the App_Data folder, create a route to an action with the [Authorize] parameter that returns a FileResult, grabbing from the App_Data folder. This locks, and on login loads the index.html file, but all of the html files resources (accompanying js and css files) fail to load because the paths are wrong. It's pulling from App_Data, but index.html is now somewhere else, away from it's included js and css folders.
Any ideas would be greatly appreciated.
You'll want to take a look at these two questions:
How to do Forms Authentication on purely HTML pages using ASP.NET?
https://serverfault.com/questions/509879/protecting-static-content-with-forms-authentication-under-iis8
Basically - ASP.NET doesn't by default handle .html content because it's much faster to have IIS handle it directly. You need to configure it correctly to have it apply to .html files so that it can then apply its authentication model.

MVC File Structure Standards

I am just new to MVC, we are building a massive system and have alot of namespacing in our site.
Where is the standard place to store files (CSS, Images, .JS) etc?
Would it be good to put them under the Content folder in sub-folders to their namespace or drop them in with their respective parent files or both.
The default project structure includes a Content folder for CSS files and a Scripts folder for JavaScript files. A lot of people use this existing template, especially since a lot of Nuget projects may rely on this.
I personally like to put all of the content in a Content folder, and have a subfolder under this named Css and Scripts. It's really a matter of preference though. Do whatever is consistent and well-organized. That will be the key to making the application more easily maintainable.
Its better to put it in saparate folder as Script(js file) at root level.
Add sub folder in content folder for Images and Css etc.
we can also create multiple controller and views for each section of your project.
like for login section you can add Authentication Controller.
we can also use Helper class for adding general function and use it in every where.
you can see following link
http://msdn.microsoft.com/en-us/library/dd410120(v=vs.100).aspx

.NET MVC - accessing .xhtml file

I have created an Area for XForms and when I try to return view("index.xhtml") the framework resolves the view as index.xhtml.aspx or index.xhtml.cshtml.
I tried routes.IgnoreRoute("{resource}.*xhtml/{*pathinfo}"); in global.asax.
Either I am not sure what URL to use (am I still hitting the controller or going straight at the .xhtml file in the views folder?) OR I made a mistake in my ignoreroute.
Any help appreciated.
If you are trying to have the action just write the content of index.xhtml, you'll need to do return File("index.html", "application/xhtml+xml"). View/PartialView assume you want the specified view file parsed and executed using the currently configured view engine.
You can't/shouldn't put static files you want remote users to be able to hit directly in your ~/Views folder. MVC places a web.config file in this folder that prevents files in this location from being served.
So, either have your controller action return the file as I mentioned above, or move the xhtml files into some other folder in your application that is not restricted. Then your route should work and your files should be served statically.

MVC Themes, Layouts and CSS

I am creating an MVC application that will effectively allow a user to upload a number of Themes to the site.
Each theme they upload will have a _Layout.cshtml file, along with css files and images.
These will all be stored in locations mapped to the userId of the person creating the uploads, i.e.:
Users/Themes/32-bit-guid/_Layout.cshtml
Users/Themes/32-bit-guid/css
Users/Themes/32-bit-guid/images
The problem I have, is that although the templates are uploaded and I can select any of the templates (which are then applied to the current logged in user), the CSS files are ignored, along with the images.
If I try to browse to the path Users/Themes/32-bit-guid/css/screen.css which I know exists, I get a resource cannot be found error.
I have had a look at creating custom view engines, ignoring routes and everything, but I am getting no where.
Has anyone experienced this before and can they point me in the right direction please?
Views in MVC require a Web.config, all I did to get the themes working was copy the default web.config file from any Views folder and put it in the same directory as my Layout.cshtml

Resources