.NET MVC - accessing .xhtml file - asp.net-mvc

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.

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.

how to serve static files via MVC after login (I am trying to add an angular mini-SPA to my large MVC project)

Looking for suggestions on how to host static files through an MVC app.
I have an MVC SPA (basically a bunch of static html, js and img files). I want users to be able to access these static files only after they have logged into my MVC application. I am running on a windows server platform, using IIS.
Currently I am doing this:
RouteTable.Routes.IgnoreRoute("AngularApp/{*path}"); //to serve up angular files from AngularApp folder
However this has a number of problems.
I don't really want to ignore the route, I want the MVC controller to check if the user has permissions (like my other controllers do), if not redirect to login page and if so, then instead of sending them to a view, allow them to load any files in a particular folder or subfolders. But the folders these files load from need to be a different path than the route URL requested. For example I don't want users to have to go to mysite.com/angularseedapp/deploy/app/mypage.html but rather if they request mysite.com/a/mypage.html I want it to serve up the file from there.
This seems simply a matter of being able to have MVC redirect and fetch files from a different folder, but I have no idea how to do this.
Could someone knowledgeable about MVC please give me a step by step simple way to do this? When I try to fetch files outside the views folder this seemingly simple task results in various permissions and other kinds of errors because I don't know how to do it correctly.
Thanks!
P.S. To clarify, I know how to get my controller to check permissions and redirect, to any single file in the views folder, but how do I do it for a whole folder of files and directories in a higher level folder? I want to map the route, have it go to a controller, then instead of going to a view I want it to take me to static files. I suspect there is some way to use maproute() in global.asax to help me do this but I do not have a lot of experience with that.
I may be oversimplifying but I usually select the application in IIS Manager and then select Mime Types, they add mappings for whatever types you want to map statically. I've done this for HTML and JSON files before and it worked fine. Use type = text/javascript or application/json etc.

ASP.NET MVC: how to bypass the controller in MVC app?

I have a .NET MVC application. So far, each page is accessed via a controller.
Now I want to direct access some cshtml files such as
http://example.org/file/abc.cshtml.
Though having .cshtml file extension, these are just pure html snippets.
How can I access these files without going through any controller.
Thanks and regards.
I would probably make a controller with an action which accepts a view name, grabs the view from the file system, and returns it as a FileResult with the mimetype text/html. You'd probably want the controller to have a hardcoded whitelist of html-fragment files, to reduce the chances that you're opening up a way for people to browse around your folder structure.
You could also look into configuring IIS to serve .cshtml files from some directories, but I'd be more concerned about accidentally opening up too much using that method.

Flash movie not made available by ASP.NET MVC

I'm working on an ASP.NET MVC app that uses a Flash movie as a banner. I'm trying to load it, however for some reason the url gets treated as an Action and the user is redirected to the login page.
The funny thing is, if I put a file in the same directory with a different extension (.txt, for example) and try to load that, it doesn't get treated as an Action.
If I point my browser to this url:
http://localhost/AppImWorkingOn/Content/Banner/banner.swf
That gets me a redirection to /Account/LogOn?ReturnUrl=..., the standard ASP.NET MVC login route.
If I point my browser to this url:
http://localhost/AppImWorkingOn/Content/Banner/banner.txt
That gets me the file. Both urls point to files existing on the server in the same directory. It's as if something is blocking the .swf filetype.
I'm using IIS on Windows 7, and checked MIME and such, but can't find the problem. How does ASP.NET MVC decide whether something's a file or a controller action?
Make MVC ignore the route for the ".swf" extension.
http://haacked.com/archive/2008/07/14/make-routing-ignore-requests-for-a-file-extension.aspx
The issue was file permissions. This was a file that came from a zip sent to me via e-mail. I dragged the folder out of the zip and moved it into place, which was probably the worst I could do. The files were individually blocked and encrypted (they turned up green) so the UrlRouteModule wouldn't treat them as files that existed and would instead insist that they're Action methods.
I should have "unblocked" the zip file before starting to copy the assets in it.

How to access HTML files from ASP.NET MVC VIEWS Folder

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).

Resources