Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ASP.NET routing to serve static files

Can ASP.Net routing (not MVC) be used to serve static files?

Say I want to route

http://domain.tld/static/picture.jpg 

to

http://domain.tld/a/b/c/picture.jpg 

and I want to do it dynamically in the sense that the rewritten URL is computed on the fly. I cannot set up a static route once and for all.

Anyway, I can create a route like this:

routes.Add(   "StaticRoute", new Route("static/{file}", new FileRouteHandler()) ); 

In the FileRouteHandler.ProcessRequest method I can rewrite the path from /static/picture.jpg to /a/b/c/picture.jpg. I then want to create a handler for static files. ASP.NET uses the StaticFileHandler for this purpose. Unfortunately, this class is internal. I have tried to create the handler using reflection and it actually works:

Assembly assembly = Assembly.GetAssembly(typeof(IHttpHandler)); Type staticFileHandlerType = assembly.GetType("System.Web.StaticFileHandler"); ConstructorInfo constructorInfo = staticFileHandlerType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null); return (IHttpHandler) constructorInfo.Invoke(null); 

But using internal types doesn't seem to be the proper solution. Another option is to implement my own StaticFileHandler, but doing so properly (supporting HTTP stuff like ranges and etags) is non-trivial.

How should I approach routing of static files in ASP.NET?

like image 235
Martin Liversage Avatar asked Jul 19 '09 12:07

Martin Liversage


People also ask

How do you serve static files in ASP.NET Core web API?

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).

Which static files can ASP.NET Core application serve?

Static files, such as HTML, CSS, images, and JavaScript, are assets an ASP.NET Core app serves directly to clients by default.

Can .NET Core HTTP pipeline be configured to server static files?

ASP.NET Core application cannot serve static files by default. We must include Microsoft. AspNetCore. StaticFiles middleware in the request pipeline.

How are static files served?

To serve static files such as images, CSS files, and JavaScript files, use the express. static built-in middleware function in Express. The root argument specifies the root directory from which to serve static assets.


1 Answers

Why not use IIS to do this? You could create a redirect rule to point any requests from the first route to the second one before it even gets to your application. Because of this, it would be a quicker method for redirecting requests.

Assuming you have IIS7+, you would do something like...

<rule name="Redirect Static Images" stopProcessing="true">   <match url="^static/?(.*)$" />   <action type="Redirect" url="/a/b/c/{R:1}" redirectType="Permanent" /> </rule> 

Or, if you don't need to redirect, as suggested by @ni5ni6:

<rule name="Rewrite Static Images" stopProcessing="true">   <match url="^static/?(.*)$" />   <action type="Rewrite" url="/a/b/c/{R:1}" /> </rule> 

Edit 2015-06-17 for @RyanDawkins:

And if you're wondering where the rewrite rule goes, here is a map of its location in the web.config file.

<?xml version="1.0" encoding="utf-8" ?> <configuration>   <system.webServer>     <rewrite>       <rules>         <!-- rules go below -->         <rule name="Redirect Static Images" stopProcessing="true">           <match url="^static/?(.*)$" />           <action type="Redirect" url="/a/b/c/{R:1}" redirectType="Permanent" />         </rule>       </rules>     </rewrite>   </system.webServer> </configuration> 
like image 109
Dan Atkinson Avatar answered Sep 21 '22 12:09

Dan Atkinson