Asp.Net Core 2.0 Web Application Static Files Gives 404

狂风中的少年 提交于 2020-12-05 03:17:33

问题


I try to get custom-extension static files from my web-server (asp.net core 2.0).

I try to run next:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDirectoryBrowser(); // just to see that I have this files 
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseStaticFiles();
        app.UseDirectoryBrowser();
    }
}

So, I can see my files at folders but I can't get it - it throw 404:

And when I click on it: nothing happened and I got 404.

Updated for Evk:

I added static_json.json file to static files at src folder and it get it as expected:

Thank you for any help in advance!


回答1:


I am not sure, but it looks like your extension is denied by IIS settings to access directly as a static resource. Please, try this for the web.config:

<configuration> 
   <system.webServer> 
       <security> 
          <requestFiltering> 
              <fileExtensions> 
                <add fileExtension=".appinstaller" allowed="true" /> 
              </fileExtensions> 
         </requestFiltering> 
       </security> 
    </system.webServer> 
</configuration> 

Also check request filtering. See more information: How to deny access to a specific file name extension




回答2:


I had the problem during development. I used the overloaded method of UseStaticFiles to set the option ServeUnknownFileTypes to true.

public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
    app.UseDeveloperExceptionPage()
        .UseSession()
        .UseStaticFiles(new StaticFileOptions
        {
            ServeUnknownFileTypes = true,
        })
        .UseDirectoryBrowser()
        .UseRequestLocalization()
        .UseMvcWithDefaultRoute();
}



回答3:


This answer might come in late, but in my case the solution was in my IIS settings. JavaScript file extension (.js) is set to False in Allowed column of Request Filtering. Removing js file from Request Filtering solved this case for me.



来源:https://stackoverflow.com/questions/48972520/asp-net-core-2-0-web-application-static-files-gives-404

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