【问题标题】:MapRoute behaving differently depending on URIMapRoute 的行为因 URI 而异
【发布时间】:2016-09-13 02:54:24
【问题描述】:

我有一个 MVC 模式,为了向后兼容网站迁移,我需要支持两个特定的 .php 文件。一个(mywebsite.com/aFolder/select.php)表现完美:使用正确的操作调用控制器。另一个没有(mywebsite.com/index.php)。在后一种情况下,浏览器只是重定向(或被重定向)到 mywebsite.com(默认 Home 控制器,Index 方法)。

我的 RouteConfig.cs:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapRoute(
        name: "php2",
        url: "aFolder/select.php",
        defaults: new { controller = "Testtt", action = "Foo" }
        );
    routes.MapRoute(
        name: "php1",
        url: "index.php",
        defaults: new { controller = "Testtt", action = "Foo" }
        );

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

}

我的 Web.config:

<system.webServer>
  <handlers>
    <add name="PhpHandler1" path="index.php" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    <add name="PhpHandler2" path="aFolder/select.php" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    <remove name="OPTIONSVerbHandler" />
    <remove name="TRACEVerbHandler" />
    <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  </handlers>
</system.webServer>

FWIW 控制器:

public class TestttController : Controller
{
    public ActionResult Foo()
    {
        return View();
    }
}

(在 Foo 中设置断点会在 /aFolder/select.php 而不是 /index.php 上命中)

这与 index.php 位于根目录而不是子文件夹有关,这使其行为不同。任何想法为什么?

【问题讨论】:

  • 我在 URL 重写方面取得了有限的成功,但仍然遇到令人沮丧的异常。只要原始 URL 及其参数仍然可用,重写到 /MyController/MyAction 会很棒。

标签: asp.net-mvc controller routes .net-4.5


【解决方案1】:

在转了几个月(断断续续)和最后几天集中精力之后,我最终找到了另一种方法 - 仍然是 URL 重写,但不是使用 web.config 规则。忽略我的 OP - 这对于我需要处理的两个 .php 页面请求来说要简单得多。在这里提供,以便它可能对某人有用。

web.config 中的处理程序:

<add name="PhpHandler1" path="index.php" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<add name="PhpHandler2" path="Foo/select.php" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

Global.asax.cs:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    string fullOriginalpath = Request.Url.ToString();
    if (fullOriginalpath.Contains("index.php"))
    {
        Context.RewritePath("/Redirect/Index");
    }
    [...+select.php, etc...]
}

为简洁起见进行了简化,未显示错误/异常处理。然后是一个名为 RedirectHandler 的标准控制器,其索引返回 ActionResult(或其他)。

【讨论】:

    猜你喜欢
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    • 2016-05-02
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多