Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make all static files like css/images/js files not be processed by asp.net mvc?

Tags:

c#

asp.net-mvc

Is it possible that static files not be processed by the asp.net mvc engine?

Can I do this at the IIS level or something? (without ofcourse creating a seperate IIS website for static files)

like image 439
Blankman Avatar asked Oct 10 '11 16:10

Blankman


People also ask

Which of the following static files can ASP.NET Core applications serve?

ASP.NET Core can serve static files—HTML files, images, JavaScript files, etc. —directly to clients. To enable ASP.NET Core to serve static files, you must use the framework's Static Files Middleware in your application, and you must specify the necessary configuration.

Which of the following middleware must be installed to serve static file in ASP NET application?

To serve static files from an ASP.NET Core app, you must configure static files middleware. With static files middleware configured, an ASP.NET Core app will serve all files located in a certain folder (typically /wwwroot).

What are static files in ASP.NET Core API project?

ASP.NET MVC 5 for Beginners Static files like JavaScript files, images, CSS files that we have on the file system are the assets that ASP.NET Core application can serve directly to clients. Static files are typically located in the web root (wwwroot) folder.

What is static file in C#?

Static files are stored within the project's web root directory. The default directory is {content root}/wwwroot , but it can be changed with the UseWebRoot method. For more information, see Content root and Web root. The CreateDefaultBuilder method sets the content root to the current directory: C# Copy.


3 Answers

You need to create an ignore route for the specific types of files you don't want to be served through ASP.NET MVC.

Add the following to your routes, for the types of files you want to ignore.

The following works for files in the root:

routes.IgnoreRoute("{file}.css");
routes.IgnoreRoute("{file}.jpg");
routes.IgnoreRoute("{file}.gif");

If you want to ignore files in a specific directory, you can do this:

routes.IgnoreRoute("assets/{*pathInfo}");

If you want to combine these into one route, you can (e.g., ignore specific types of files in a directory):

routes.IgnoreRoute("{assets}", new { assets = @".*\.(css|js|gif|jpg)(/.)?" });

This overload of IgnoreRoute accepts a url (the first argument) and a Constraints object of things to ignore.

Since the RouteConstraints in ASP.NET MVC can be implemented multiple ways (including a regex), you can put standard regexes in the second argument.

If you want to implement a custom constraint, there is lots of useful documentation on that subject (say, if your constraint is dynamic at runtime).

like image 127
George Stocker Avatar answered Oct 28 '22 17:10

George Stocker


Be careful, @george-stocker's answer works for static files in the root directory only!!!

To catch static files in all possible directories/subdirectories, use an ignore rule with a "condition", like this:

routes.IgnoreRoute("{*allfiles}", new { allfiles = @".*\.(css|js|gif|jpg|png)" });
like image 45
Alex from Jitbit Avatar answered Oct 28 '22 18:10

Alex from Jitbit


Static files are not processed by ASP.NET MVC, unless you have a route that matches the URL of a static file. Maybe you are asking about static files processed by ASP.NET, in that case you should not use runAllManagedModulesForAllRequests="true". Here's a post with more info.

like image 28
Max Toro Avatar answered Oct 28 '22 17:10

Max Toro