【问题标题】:Routing of test.html file -MVC return 404test.html 文件的路由 -MVC 返回 404
【发布时间】:2014-04-28 14:23:41
【问题描述】:

我的目标是通过用户代理将请求 test.html 的请求重定向到 test2.html,我使用 MVC,所以我关注 this 文章

所以在我的 /App_Start/RouteConfig.cs 中是:

 routes.RouteExistingFiles = true;
 routes.MapRoute(
        name: "Test",
        url: "test.html",
        defaults: new { controller = "Home", action = "Html", page = urlParameter.Optional }
    );

在 webconfig 中我放置了以下节点:

<add name="HtmlFileHandler" path="test.html" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

但是,当我更改 IIS 中的调试设置并请求 localhost:96/test.html 时,未命中操作中的断点并返回 404 未找到

从我上面链接的文章中,我想我做了所有的事情,我也用谷歌搜索了很多,这应该是它,但它不是......我可能会强迫一些错误。我会很高兴有任何建议!如果您认为它将依赖于某些版本的 SW 或 OS,我可以在此处填写它们

【问题讨论】:

  • 你在 homecontroller 中创建了 html 动作吗?
  • 是的,我做到了。但即使我没有做到,也应该有例外而不是 404
  • 如果我不采取行动,我总是会得到 IIS 404,但如果我不创建视图,我会得到一个异常
  • 奇怪但控制器和动作在那里:-/
  • 您可以尝试将路由移动到配置的顶部,使其成为第一条规则,看看是否有任何改变 - 有时其他规则可能会导致错误

标签: c# html asp.net-mvc asp.net-mvc-routing


【解决方案1】:

在您的地图路线 URL 上,明确标识文件的路径:

url: "folder1/folder2/{page}.html

然后尝试在您的控制器上进行此操作:

 public ActionResult StaticPage()
        {
            var name = new Uri(Request.Url.ToString()).Segments.Last();
            return new FilePathResult(name, "text/html");
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多