Using a wwwroot folder (ASP.NET Core style) in ASP.NET 4.5 project

后端 未结 3 1245
-上瘾入骨i
-上瘾入骨i 2020-12-01 05:44

I quite like the approach of the new asp.net (asp.net 5\\core 1.0) web apps with the wwwroot folder being the root and only static files in there being served up.

3条回答
  •  萌比男神i
    2020-12-01 05:57

    I believe I have a working method for doing this now. Took a bit of googling and experimentation, but in the end I came up with the following process:

    1. Create a new ASP.NET 4.5 project in VS2015, selecting the Empty Template

    2. Add OWIN references through nuget (Install-Package Microsoft.Owin.Host.SystemWeb and Microsoft.Owin.StaticFiles)

    3. 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);
              }
          }
      }
      
    4. 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:

      
          
            
            
          
        
      

    I'm always open to better suggestions on how to do this, but this at least seems to work for me.

提交回复
热议问题