2016-09-13 24 views
0

我有一個MVC模式,爲了向後兼容網站遷移,我需要支持兩個特定的.php文件。其中一個(mywebsite.com/aFolder/select.php)表現完美:使用正確的操作調用控制器。其他不(mywebsite.com/index.php)。在後一種情況下,瀏覽器只是重定向(或重定向)mywebsite.com(默認主控制器,索引方法)。MapRoute根據URI行爲有所不同

我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(); 
    } 
} 

(設置裏面美孚斷點被擊中的/ aFolder/select.php但不在/index.php上)

這是關於index.php是在根,而不是一個子文件夾,使其行爲有所不同。任何想法爲什麼?

+0

我在URL重寫方面取得了有限的成功,但仍然遇到令人沮喪的異常。只要原始URL及其參數仍然可用,重寫到/ MyController/MyAction會很好。 – GeoffM

回答

0

經過幾個月(開啓和關閉)以及最後幾天的密集循環後,我最終找到了另一種方式 - 仍然是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(或其他)的索引。

相關問題