How do I serve static files from mvc without using content folder? - asp.net-mvc

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.

Related

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.

having multiple mvc projects under same url

I was wondering if it's possible to create an mvc project with layers inside or even multiple projects using the same web address.
Like if I have a website called mysite.com which have a website project doing sine stuff.
But then I have a webproject which I'm doing some experimentational code on which I whould like to find via the url "mysite.com/test". This whold be a dufferent mvc project. Is that even possible?
It is possible, what you actually need to is create a new project and define it inside a virtual directory.
MVC is based on global.asax and web.config configurations and you can't have 2 separate projects inside the same application. In the scenario you described, it sounds like you need to create a "Virtual Directory" in IIS for the second application instead of making just a "Sub Folder".
The Virtual Directory will allow you to declare a new web.config and global.asax where you set the new routings and configurations method for your second application and this will also give you the freedom for independent updates without affecting your main project.
For a full walkthrough on how you can perform this please refer to this site:
http://msdn.microsoft.com/en-us/library/vstudio/bb763173(v=vs.100).aspx

Serving static files in development with IIS Express and Visual Studio 2012

This seems like a pretty common use case, but I can't for the life of me find any information on how to set this up.
I've got an "Admin" site where I upload images and then my normal website (separate project) where I need to display those images. Both projects need to be able to display the images (so I can't just simply store them in the website's project directory and call it a day).
In production, this is a no-brainer. I just set up a virtual directory in IIS, but developing in Visual Studio, there's no direct support for this. I've seen things online that tell you do go into the IIS Express config files and add the virtual directory manually, but there's two huge problems with this: 1) I'm in a multi-developer environment and making every developer do this in a consistent way manually is a no-go and 2) there's no automated way to reference this static directory in my code, so that's going to entail a lot of hard-coding paths to image references, which is a hugely bad idea.
I'm aware, also, that I can create links in my project, but I'm dealing with uploads, not static resources that I know about at design time, so creating links for every new upload manually in my project (especially when this is only for testing in development) is insane.
So what is the best way to dynamically share resources between projects just for the purposes of development in Visual Studio?
For what it's worth, these are both ASP.NET MVC 4 projects and I'm using Visual Studio 2012 on a Windows 7 box.
EDIT I should specify that upon uploading the image, a relative path to that image is stored in a database table. I suppose my real issue is in resolving that partial path into a real accessible URL from an entirely different project. If I was in the same project, I would simply to something like Url.Content(Model.ImageUrl), but this obviously won't resolve if the image is not actually in the same project.
It was pointed out to me that I'm over-thinking this.
In production, the files would of course be served through some virtual directory, but in development, it doesn't matter where they're served from. So, all I really need to do is add an app setting for the static URL prefix to prepend to the relative image URL.
For development it would just be the localhost + port of my admin site.
Then, in my release web.config transform, I would just change it to the production URL, i.e. something like "http://static.mydomain.com". In my app code, I just look up this app setting and prepend it to my relative image URL and I'm golden.
If I run into any problems with this approach, I'll note them here with appropriate solutions, once I get a chance to actually implement it.

MVC Razor Shared external layout

I'm looking for a way to reference a shared external _layout.cshtml from MVC 3 and Razor.
A little back story:
We have multiple developers. All of them are working on separate MVC applications that all need the same look and feel. All these applications will be deployed to the same site for example
http://www.example.com/App1/
and
http://www.example.com/App2/
The look and feel will be generated by the CMS and dropped into a different folder
http://www.example.com/Layout/_layoutExt.cshtml
I've tried
MasterName = #"C:\inetpub\wwwroot\layout\_LayoutExt.cshtml";
But it gives me the error that it can't find the file
The view 'Index' or its master was not
found or no view engine supports the
searched locations. The following
locations were searched.
My eventual solution was to use Symbolic links (or junctions or hard links) to link the needed file into the view folder. This way the CMS writes to one location and my app reads from another. Not the clean solution I was looking for.
You'll probably need to make your own view engine that support reading file outside the web application home directory.

ASP.NET deployment - How to share BIN across multiple WebApp Projects?

What is the best practice for sharing the assemblies of a bin folder across multiple ASP.net websites in IIS 7?
I've got several sites, each with slightly different HTML front ends, but all with the same middle tier logic and DB. I don't want to redploy the same dlls to each of the many site's bin folders everytime I make a change.
Thanks.
Yes, actually, you do want to deploy them individually. That's how it works.
If this truly offends you, then consider whether this common code should be in a WCF service called by all those sites.
Another option is to make your .Net code smart enough so that it can load the correct HTML front end based on the URL. That way you could deploy your code to one location, then have multiple virtual directories use the same code.
You could possibly try putting them in a common folder location and then change the assembly probing paths to look in that location.

Resources