How to configure rout to serve all files from subfolder using httphandler in asp.net mvc? - asp.net-mvc

I have two websites in IIS under default web site. Lets say "Website1" page gives call to "webiste2" which return some content. Entry URL of website2 is http://domainname/websitename/controller/action this action return html(index.html). structure of website2 is as bellow.
Website
Folder(websitename)
........Folder A(folder_{id}(id is dynamically generated) is under folder A)
..............Folder_{id} --> this folder contains files with many extensions
........Index.html->this file has references to above folderA &subfolders files
........Web.config
Now i want to configure route such a way that that serve all types of files extensions for url like below.
websitename/folder A/filename.abc
websitename/Folder A/folder_1/filename.xy
websitename/folder A/Folder_1/filename.mp3
websitename/folder A/Folder_1/filename.png
There can be thousands folder under folder A. so the folder_1 value can be changed from folder_1 to folder_2, folder_3 .............. folder_1000 or as many folders.
I want generic rout to serve all dynamically generated folders and its files.
There are many files with different extensions under these dynamically created folder or we can see its as package of different files like .mp3, ,jpg, .swf, .js...
Please guide me to write rout to achieve above. Or you can provide links so that i can go through.

Simply, don't. Static files are supposed to be served directly by IIS. By default, in MVC, anything that has an extension (.*) is handled by IIS, and MVC is never even involved. If you've customized something, I'd recommend undoing that.

Related

How to serve some static files of a certain file type, but not others?

My ASP.NET MVC project's root directory contains some typical static files, like robots.txt, manifest.json, browserconfig.xml, etc. If I'm not mistaken, each of these examples I listed should be able to be served with no involvement from MVC via GET requests to the root directory (i.e. mysite.com/manifest.json — if that's not true, please let me know).
I know from this answer that I can configure this behavior per file type in the Web.config. My question is, what if there are other .json files in my root directory that I don't want to serve, like compilerconfig.json or bundleconfig.json (both files generated by IDE tools)? What's the best way for the application to be able to serve some files of type X, but not others?
You can always ignore them via routes:
routes.IgnoreRoute("{somefilename}.json");
Another alternative would be to move the files you don't want to be served to another folder and add a web.config file to it to manage what gets served (or doesn't).
I am sure there are other ways. Modules come to mind...

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.

What files should go in the ASP.NET MVC Content folder?

I see that ASP.NET MVC generates a Content folder by default, which stores the site.css file. I have a site that uses some image files and XML files in order to do its job, yet I do not want to have those files as embedded resources. Should I just put the files in the solution within the content folder? Should I turn off the "Do not copy" build option on each resource since I will need to deploy the files to the website? Or should these files not even live in the solution and be loaded from the file system?
It's a good approach to add all your files/resources to the solution (at least you can see them in VS ;-) Other developers won't be surprised that your solution really needs some other files.
If those files are the 'content' of your website (like css files) then you can store them in the Content folder. It is up to you what folder structure you come up with. For example you might want to store your images in folder related to the modules of your application, or just all in the images folder. MVC doesn't enforce any particular way of keeping your resource files.
Just leave the Build Action set to Content and Copy to Output Directory to 'Do not copy'.
You might want to take a look at T4MVC project (also available as NuGet package) which will help you in keeping urls to your images / files compile-time safe.

ASP.NET MVC routing and static data (ie. images, scripts, etc)

If I have a request for a resource in my ASP.NET MVC1 (or 2) web app for a static resource, say ... an image or a javascript file or a css file ... does the .NET framework try and see if the request matches the route list ... and eventually can't find a controller for it?
eg.
Resource: /Content/Images/Foo.png
Does this request go through my route list .. fails to match any controllers / actions to this request and then attempt that path directly?
You can choose whether to map an existing file or not setting the RouteCollection.RouteExistingFiles Property
Gets or sets a value that indicates
whether ASP.NET routing should handle
URLs that match an existing file.
Here is what I read from here:
However, the routing system still does check the file system to see if an
incoming URL happens to match a file or disk, and if so, routing ignores the request (bypassing
any route entries that the URL might also match) so that the file will be served directly.
This is very convenient for static files, such as images, CSS, and JavaScript files. You can
keep them in your project (e.g., in your /Content or /Script folders), and then reference and
serve them directly, just as if you were not using routing at all. Since the file genuinely exists
on disk, that takes priority over your routing configuration.
If, instead, you want your routing configuration to take priority over files on disk, you can set
the RouteCollection’s RouteExistingFiles property to true. (It’s false by default.)
By default the routing engine will ignore route maps for all files that exist physically on the server. In short, you need to do nothing for a MVC app to link to static files.
You can also do a little trick in IIS. I store my js, images, css etc in the Content folder underneath the virtual directory.
If you then view properties (in IIS manager) of the Content folder, create it as a virtual directory, then remove the Wildcard mapping. Then set the Content folder back to a normal directory. This should then stop requests to these files being handled by the aspnet_isapi handler.

Include MVC views and master pages as DLL resources instead of separate files

Does there exist a method when publishing an ASP.NET MVC application to completely remove the .aspx view files (and if possible .master too) by compiling them into the application DLL as resources?
The published application would just be the /bin folder, Global.asax and web.config, a default.aspx if needed, and whatever is in the /Content folder. All the views would be contained in the MyProject.dll file.
To clarify I don't mean where every .aspx is overwritten with a 1 line dummy file referencing the correct resource, but where those files can be eliminated entirely in the published app.
The goal here is to simply change management and all the auditing and layers of people surrounding it - one file gets deployed, that file replaces the existing file, and no messing around with a stack of .aspx files that have to be added/removed/updated (yes, SVN or a similar solution handle that problem instead, but management politics prevent this).
Is this what you are looking for?
It's possible with the web forms view engine but you'll have to extend the path provider yourself.
Here is a question here at SO about the same thing:
Using VirtualPathProvider to load ASP.NET MVC views from DLLs
If you use the Spark view engine, it already has additional path providers built in.
The documentation can be found here:
Adding a view folder to config
It allows you to locate your views inside a DLL as an embedded resource, somewhere else on the file system, using the default virtual directories, or plug in your own custom provider.

Resources