Static file not served through IIS but serving through localhost - ASP.NET Core 3.1 MVC & C#

烂漫一生 提交于 2021-01-28 19:34:09

问题


I am new to programming, and I was able to solve most of the issues all by myself. But this issue goes over my head.

I am using Visual Studio to run my app locally. When I click on "IIS Express" within VS to run the app, the app is opening in a browser with url http://localhost:1234/ and serving static files.

However when I use IIS to host my app, like when I type myapp.com in my browser, I am seeing website in plain text. And no static files (css, js) are being served.

Not sure what is causing the issue. I googled for long time and read lot of posts, but most of them are asking to check if IIS has static role service turned or suggesting to use app.UseStaticFiles() within startup.cs file. Which in my case is done.

program.cs:

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
              .ConfigureWebHostDefaults(webBuilder =>
              {
                  webBuilder.UseStartup<Startup>();
              })

Startup.cs:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseDefaultFiles();
    app.UseStaticFiles();
}

Any help is much appreciated.


回答1:


I didnt know that when we publish it to "files and folder" from Visual studio, any changes that we make in Visual studio will not go through to published folder. Because of this reason "app.UseStaticFiles();" command (that I wrote after publishing) is not passed on to published folder. I had to delete the published folder and republish to make everything work fine.



来源:https://stackoverflow.com/questions/61606531/static-file-not-served-through-iis-but-serving-through-localhost-asp-net-core

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