2016-11-25 154 views
1

在MVC 5中,我有一箇舊的控制器「MyOld」並需要它指向「MyNew」,但請求繼續到MyOld。MVC與區域的路由

 routes.MapRoute(
      name: "newthing", 
      url: "Trade/MyOld", 
      defaults: new { controller = "MyNew", action = "Index", area = "Trade" } 
      ).DataTokens.Add("area","Trade"); 

回答

0

您應該添加路由器的方法RegisterArea

TradeAreaRegistration.cs

public class TradeAreaRegistration : AreaRegistration 
{ 
    public override string AreaName 
     { 
      get 
      { 
       return "Trade"; 
      } 
     } 

    public override void RegisterArea(AreaRegistrationContext context) 
    {  
     context.MapRoute(
     name: "MyOldToMyNew", 
     url: "Trade/MyOld", 
     defaults: new { controller = "MyNew", action = "Index", id = UrlParameter.Optional } 
    ); 

     context.MapRoute(
      name: "Trade_default", 
      url: "Trade/{controller}/{action}/{id}", 
      defaults: new { action = "Index", id = UrlParameter.Optional } 
     ); 
    } 
}