最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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.

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.

Share asked Jun 5, 2016 at 22:20 Rafael ValleRafael Valle 1512 silver badges10 bronze badges 2
  • Just curious! why would you need above code for handling the static files ? The MVC project that I run does have a MVC application, WebAPI endpoints and Angular JS static files. Everything seem to work without above code. – David Chelliah Commented Jun 5, 2016 at 22:30
  • 1 i am not using mvc, i have a separete project for the angularjs2 and i only want to have a build of it under wwwroot – Rafael Valle Commented Jun 5, 2016 at 22:49
Add a ment  | 

2 Answers 2

Reset to default 3

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.

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论