Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vadymberkut/1f052679cd8786993e7f31b7eed76741 to your computer and use it in GitHub Desktop.
Save vadymberkut/1f052679cd8786993e7f31b7eed76741 to your computer and use it in GitHub Desktop.
ASP NET MVC Using wwwroot static files
Create a new ASP.NET 4.5 project in VS2015, selecting the Empty Template
Add OWIN references through nuget (Install-Package Microsoft.Owin.Host.SystemWeb and Microsoft.Owin.StaticFiles)
Add a startup file similar to this:
[assembly: OwinStartup(typeof(MyApp.Startup))]
namespace MyApp.UI
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
string root = AppDomain.CurrentDomain.BaseDirectory;
var physicalFileSystem = new PhysicalFileSystem(Path.Combine(root, "wwwroot"));
var options = new FileServerOptions
{
RequestPath = PathString.Empty,
EnableDefaultFiles = true,
FileSystem = physicalFileSystem
};
options.StaticFileOptions.FileSystem = physicalFileSystem;
options.StaticFileOptions.ServeUnknownFileTypes = false;
app.UseFileServer(options);
}
}
}
Add the following to your web.config file, to prevent IIS from serving static files you don't want it to, and force everything through the OWIN pipeline:
<system.webServer>
<handlers>
<remove name="StaticFile"/>
<add name="Owin" verb="" path="*" type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler, Microsoft.Owin.Host.SystemWeb"/>
</handlers>
</system.webServer>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment