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)
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.
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).
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.
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.
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).
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)" });
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With