How to serve static files from ASP.NET-MVC 4 app? - asp.net-mvc

This is the first time I've ever developed an ASP.NET app.
I need to serve some static files from my ASP.NET 4 app. The trouble is, I want these files to appear as if they're at the root of the app, when actually they're several folders down. With ASP.NET Core this is easy; you just drop the files in the wwwroot folder, and the server treats these files as if they were at root. But my boss has asked me to use ASP.NET 4, instead of Core, if possible. So I need to figure out how to configure this.
This post and this post both answer the question I have, but I don't understand the answers that are given. Both answers tell me to add some functionality to the "ConfigureStaticFiles" method in my "Startup" class. But I don't have a Startup class.
So, what is the best way to do this in ASP.NET 4? Should I add a Startup class? If I add a Startup class, what do I do with my Global.asax? Or should I approach this some other way?

If you want ALL static files to be served from MVC (rather than IIS) you add this to your web.config
<modules runAllManagedModulesForAllRequests="true">
...under <system.WebServer>
further read #here

Okay.
I was able to use URL rewrites to make the files and folders in a specific subdirectory appear as if they were in the root folder. This answer provides an implementation for that functionality, although my app's architecture necessitated a slightly different configuration.
I don't know if URL rewrites are the best way to accomplish this functionality, but it's what I'm using for now.

Related

What is the equivalent of app.config in MVC 4

I have only few projects on my bag. In a previous desktop application when I wanted to store and use some application specific information I was using the app.config file.
Now I work on ASP.NET MVC 4 application and again I want to store some application specific information but this time I'm not sure. I have the web.config file which seems like a good place for this purpose but I'm not sure if it's the right place to store custom information there.
What is the right approach to do this? For example I want to save and extract path to directory on the file system where I'll save all my files. In ASP.NET MVC 4 what/where is the right place to do that?
The web.config file is what web application use where a desktop application uses app.config, and it's a good place to put application specific information.
You can add keys to the <appSettings> tag, and use them in the application just as you would if you put them in that tag in the app.config file. Example:
string path = ConfigurationManager.AppSettings["DataPath"];
Web.config is for ASP.NET and ASP.NET MVC; app.config is for desktop applications and DLLs.
If you need to storie any kind of parameter for your application that you can change without having to recompile, Web.config is the place to go.
Web.config is the web application version of app.config. Web.config seem like a good place for the configuration settings you're talking about.
Sometimes when you have a dll that had reference to desktop app.config, and when you import that MVC application you need to change the configuration data that used to be in app.config now to web.config.

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.

Hosting an MVC and a webforms site on the same IIS7 instance - web.config inheritance

We have a website that was written in classic ASP, then I started to extend it using web forms. These extensions exist in a subfolder of the main folder. Now we've decided we'd prefer to use MVC3. Also, as we'd like to convert all our site to MVC3 over time, we are hosting the MVC code in the application root. I've found some other questions where people have a similar issue to mine, but no solution. The issue is simply that my web forms app can't seem to be stopped from inheriting the web.config settings from the root folder, and as a result, it won't run, it either complains about missing dlls, or complains about running the wrong version of .NET, or complains I need to remove some settings ( which I try and can never get to work right ). The app in the subfolder is also hosting a webservice that is called by our application, and it also runs HTTP handlers to protect our imaging content, so it's got a bit of stuff in it. Do I need to run my MVC site in a subfolder ? Is there any way to have MVC in the folder above a web forms app ? I'd prefer to set things up so they share session data, but that's looking likely to be impossible at this stage...
So to be clear the folder structure is:
<root>
contains asp site and MVC site.
<subfolder>
contains webforms application
</subfolder>
</root>
and my issue is getting the subfolder to run, preferably in the same session as the MVC app.
There is no reason you can't run regular .aspx files on an MVC site. You are correct though, web.config settings are inherited from the parent (chain), but you just add a new web.config in your directory with relevant settings.
What you will have to do is play with the routes, because by default MVC will route all requests into your controller classes. But if you google around its fairly simple to add an exception to the routing.
If you post some of the specific errors we can probably help further.
Oh and do you mean Classic ASP? i.e. not Classic ASP.NET? Because you'll have fun sharing session data between ASP & ASP.NET.

Prevent IIS7/ASP.NET locking directory with images

I have an ASP.NET webapp using the ASP.NET MVC 2 framework. It allows users to upload files to an uploads folder. The issue occurs when an image within a sub-folder is accessed by a web browser:
http://mywebapp/uploads/image_gallery/sub_folder/image.jpg
The uploads folder is static and can't be modified by users, but anything below it is intended to be modifiable.
In the above example, the image_gallery folder becomes locked because w3wp.exe appears to create a handle on the sub_folder directory (using process explorer by sysinternals). I am still able to rename the sub_folder directory and the handle seems to stay with it after a rename, but i can't rename the parent folder (image_gallery in this case).
I can still browse within the folder and view other images and files etc. But can't rename the parent folder.
As this is using the MVC 2 framework i've put in an exclusion for the uploads folder like so:
routes.IgnoreRoute("upload/{*pathInfo}");
into global.asax, so i'm assuming that ASP.NET is serving up those images directly (outside of the MVC framework)
So I guess my question is, is there any way to prevent IIS from putting a handle on specific directories or forcing it to remove a handle? Is the MVC 2 framework doing something tricky even though i have the ignoreroute specified?
Thanks in advance for any tips!
I had the same problem, and after much investigating I've found the culprit:
Web.config
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
Setting this option (which makes all http modules run on ALL files, static ones included) to false fixed it for me.
I believe, directory handle gets created in worker processor because ASP.NET typically watches the file system for changes - this allows it to respond to change/addition of config files, new dlls etc.
I suggest that you move from the concept that users can control physical directory structure on the web server. Typically, what user can create is an logical directory structure but physical structure would be controlled by your program logic. Your logic will store the logical structure created by user and its mapping with actual physical structure on the web server.

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