【发布时间】: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