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

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.

Related

How to configure rout to serve all files from subfolder using httphandler in 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.

AngularJS + ASP.NET MVC: routing URLs without corresponding physical view

Context
We have a web app using AngularJS with ASP.NET MVC. It aims to replicate a file-system structure through URLs, like this:
http://(site1)//rootFolder//NFolders//File
For views in Angular, we are using stateProvider, with different states associated to existing views in disk (.cshtml files).
The problem issues when we request a file or folder from the browser and we try to serve a virtual view that doesn't really exist. We have just been able to achieve it with a full postback and some hacks, making a complete reload on each file/folder request.
Question: Is there a way of achieving that in Angular? Other way, the request for different URLs which don't have either their corresponding physical view as a file or any state in stateProvider, handling them with Angular and customizing a specific view which would deal with disk routes requested by URL?
RE-EDIT
We will have:
a fixed root view for the website: http://website
several root projects with a name:
http://website/site1
http://website/site2
....
http://website/siteN
each site may have different files and folders accesible via the URL:
http://website/site1/folder1/folder2/.../foldern
http://website/site1/folder1/folder2/.../foldern/fileX
RE-QUESTION: is it possible to handle this "virtual" URLs without having a physical view behind for folders or files? Our idea is to have one unique view called siteController which would control all the sites, and paths to folders and files.
I would suggest you define rootFolder/NFolders/File as parameters. Then you can have a html file as the view template to populate data from server.
Have a look at this link
https://github.com/angular-ui/ui-router/wiki/URL-Routing#stateparams-service

How do I serve static files from mvc without using content folder?

I want to be able to have a folder which allows regular access like the \content folder except that it holds a ClickOnce application. I can't seem to be able to achieve this using Mvc, but I'd like to have this folder accessible without Mvc seeing it as a controller action.
I tried using routes.Ignore(theUrl), but this seemed to have no effect.
There are two ways you can do this. The first is where you are currently going, which is to satisfy it with routing. You should be able to use the following to ignore the intended route:
routes.IgnoreRoute("...")
However, this might not be the right approach from a security stand point. I would recommend you define an explicit action to download your click-once exe. Have a look at this q/a as an example of using the FileContentResult class.
The reason for this is that you can control security for that file without having to open up access levels to other directories.
Edit: If this is for an entire directory, you can still follow this same approach.
Set up the folder as a virtual folder in the website on IIS. then you can set the url in the code to point to the machine serving the request and to the virtual folder on the web server.

asp mvc 2 route all requests to files in specific folder to given controller/action

Can't make this work fast. I have a folder in which I have text files.
And so if user requests some file like mysite.com/thefolder/file.txt he sees the contents of the file.
What route should I register so that all requests to anything in the folder would go to mysite.com/Error/NotAllowed action?
IIS will, by default, serve all .txt files directly without even going to the ASP.NET handler, which handles routing and MVC.
To change that behaviour, you will have to change your IIS settings or change the routing rules at any firewall/load balancer you have in front of it.

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.

Resources