ASP.NET 4.6 WEB API, Not serving static files(.js, .css) IIS express visual studio

百般思念 提交于 2019-12-08 11:37:31

问题


I have been looking over the web and still no luck, hence why I am asking. I have a WEB API 4.6 project that will also be home to an angularjs2 application, I have set up to use static files like this:

 var physicalFileSystem = new PhysicalFileSystem(@".\wwwroot\");
        var options = new FileServerOptions
        {
            EnableDefaultFiles = true,
            FileSystem = physicalFileSystem,
        };
        var contentTypes = new FileExtensionContentTypeProvider();

        options.StaticFileOptions.FileSystem = physicalFileSystem;
        options.StaticFileOptions.ServeUnknownFileTypes = true;
        options.StaticFileOptions.ContentTypeProvider = contentTypes;
        options.DefaultFilesOptions.DefaultFileNames = new[] { "index.html" };
        options.EnableDirectoryBrowsing = true;
        app.UseFileServer(options);

index.html is served, when index.html tries to fetch all the files(.css.js) a 404 error shows up in the console. I have enabled directory browsing and I see that the files are there.


回答1:


I found that my code works well, I had to just add the following code to my web.config file.

<system.webServer>
<handlers>
  <remove name="StaticFile"/>
  <add name="Owin" verb="" path="*" type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler, Microsoft.Owin.Host.SystemWeb"/>
</handlers>

found the answer here. I believe this could be marked as a duplicate or deleted.




回答2:


Instead of using OWIN you can accomplish your goal of serving static files from ~/wwwroot with a web.config setting (URL rewrite) found here.



来源:https://stackoverflow.com/questions/37647350/asp-net-4-6-web-api-not-serving-static-files-js-css-iis-express-visual-stud

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!