【问题标题】:ASP MVC allow static html filesASP MVC 允许静态 html 文件
【发布时间】:2017-05-08 05:37:30
【问题描述】:

我有一个简单的 ASP MVC 项目。 在此站点下,我需要显示纯 .html 文件,而不将路由视为 controller/action

我有一个文件夹 documentation,里面有文件 index.html。 我需要在 www.mydomain.com/documentation 下访问它,它返回 403。 目前它仅适用于 www.mydomain.com/documentation/index.html

我已经添加了

routes.Ignore("developers/");

转入RouteConfig.cs

Startup.cs 中用于 OwinAppBuilder

        app.UseStaticFiles();

我应该怎么做才能在 www.mydomain.com/documentation/index.html 下访问它?

【问题讨论】:

  • 您能否提供其他静态文件,例如 .js 和 .png?另外你运行的是什么版本的 IIS?
  • 也不是核心?对于核心,请参阅:stackoverflow.com/a/41093539/240564
  • 好吧,我实际上可以显示文件,问题是我需要将“index.html”显式写入 URL...当我禁用所有 MVC 并让网站运行时,一切正常跨度>

标签: c# html asp.net asp.net-mvc static-files


【解决方案1】:

在您的路线配置中添加

routes.RouteExistingFiles = true;
routes.MapRoute(
    name: "staticFileRoute",
    url: "Public/{*file}",
    defaults: new { controller = "Home", action = "HandleStatic" }
);

在你的web.config下<system.webServer>/<handlers>添加

<add name="MyCustomUrlHandler2" path="Public/*" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

如果您想了解更多信息,请从this 网站获取。

编辑:请注意,MapRoute 中的 url 在 url 中有目录 Public,在 web.config 行的 path 中也有。如果您的 html 文件不在名为 Public 的目录中,则需要更改该部分以匹配您的目录结构。

【讨论】:

  • 好吧,这可以处理 domain.com/documentation/index.html 之类的路由。但我需要相反的情况,我需要将 domain.com/documentation 之类的 url 转换为 domain.com/documentation/index。 html, 显示html文件
猜你喜欢
  • 2018-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 2014-05-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多