Serving static files in web.api

Recetly switching from mvc to a web.api app and we had to serve some static files trough web api. However, web.api does not support serving selfing files on its own, for a good reason.

Web.Api was built on top of owin and is really only focused on serving web api requests. If you do not know about owin, I suggest you google about it right now, there is also an awesome training on pluralsight about it.

So as web.api is sitting on top of owin, this means we can easily integrate a static file middleware to our api pipeline.

To do so, simply install the Microsoft.Owin.StaticFiles package from nuget.

Install-Package Microsoft.Owin.StaticFile

Now that you have static file middleware assemblies installed, you need to configure static folder you want to serve in your owin startup class.

If you do not already have a startup class, simply add one to your main assembly

Now have your startup file look like this

public void Configuration(IAppBuilder app)
    app.UseStaticFiles(new StaticFileOptions()
        FileSystem = new PhysicalFileSystem(OwinEnvironment.MapPath("c:\\pathToYourAssets")),

        RequestPath = new PathString($"/assets")


    return app;


There you go, if you hit localhost/assets in your browser you should be able to get your assets.

Submit a Comment

Your email address will not be published. Required fields are marked *

Share This