.NET IIS发布项目后设置虚拟路径访问文件 404

解决方案:

找到Startup.cs中适当配置静态文件中间件:

确保调用了UseStaticFiles中间件

cs 复制代码
public void Configure(IApplicationBuilder app)
{
    app.UseStaticFiles(); // 确保这行在UseRouting之前

    app.UseRouting();

    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers(); // 你的自定义路由
    });
}

再检查Web.config配置文件里有

cs 复制代码
<location path="." inheritInChildApplications="false"></location>

假设有一个网站,其根目录下有多个子应用程序,你可能想为根网站设置一些特定的HTTP头部,而不希望这些设置应用到子应用程序。你可以在web.config文件中这样配置:

cs 复制代码
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <httpProtocol>
        <customHeaders>
          <add name="X-Frame-Options" value="SAMEORIGIN" />
          <add name="X-Content-Type-Options" value="nosniff" />
        </customHeaders>
      </httpProtocol>
    </system.webServer>
  </location>
</configuration>

这样静态文件请求(如图片、CSS、JavaScript文件)就不会被路由系统拦截。

<location path="." inheritInChildApplications="false">是一个强大的配置工具,用于在IIS中对根应用或网站进行精细控制,而不干扰子应用的配置。

相关推荐
TG:@yunlaoda360 云老大19 小时前
华为云国际站代理商GeminiDB的企业级高可用具体是如何实现的?
服务器·网络·数据库·华为云
j_xxx404_19 小时前
Linux:第一个程序--进度条|区分回车与换行|行缓冲区|进度条代码两个版本|代码测试与优化
linux·运维·服务器
沙滩小绵羊1 天前
Linux常见命令
linux·运维·服务器
誰能久伴不乏1 天前
epoll 学习踩坑:`fcntl` 设置非阻塞到底用 `F_SETFL` 还是 `F_SETFD`?
linux·服务器·网络·c++·tcp/ip
阿巴~阿巴~1 天前
告别命令行:Navicat可视化操作数据库(附安装使用教程)
服务器·数据库·mysql·navicat·可视化操作数据库
TG:@yunlaoda360 云老大1 天前
华为云国际站代理商跨境容灾是如何实现的?
服务器·数据库·华为云
云川之下1 天前
【网络】华为交换机S3700与S5700详解
服务器·网络·华为
咕噜签名-铁蛋1 天前
云服务器故障服务保障体系构建与实践
服务器