2015-02-05 80 views
0

我有一些問題,重定向到一些行動基本上,如果我沒有參數路線或參數調用Id它工作正常,但是當我添加一些不同的參數路由RedirectToAction不起作用,它拋出錯誤:路由表中沒有路由與提供的值匹配。RedirectToAction參數在路線

[Route("First")] 
public ActionResult First() 
{ 
    //THIS DOESN'T WORK 
    //return RedirectToAction("Second", new { area = "plans"}); 

    //THIS WORKS 
    return RedirectToAction("Third", new { id = "12" }); 
} 

[Route("Second/{area}")] 
public ActionResult Second(string area) 
{ 
    return new ContentResult() { Content = "Second : " + area}; 
} 


[Route("Third/{id}")] 
public ActionResult Third(string id) 
{ 
    return new ContentResult() { Content = "Third " + id }; 
} 

所以,當我輸入/首先它正確的重定向到第三,但到第二它拋出錯誤。

我沒有任何額外的東西在RouteConfig剛:

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
    routes.MapMvcAttributeRoutes(); 
} 

回答

2

也許,你與area路由參數發生衝突area參數。 Areas is a resource from Asp.Net MVC,定義如下:

Areas are logical grouping of Controller, Models and Views and other related folders for a module in MVC applications. By convention, a top Areas folder can contain multiple areas. Using areas, we can write more maintainable code for an application cleanly separated according to the modules.

你可以嘗試重命名這個參數,並使用它,樣品,嘗試重命名areaareaName

[Route("First")] 
public ActionResult First() 
{ 
    return RedirectToAction("Second", new { areaName= "plans"}); 
} 

[Route("Second/{areaName}")] 
public ActionResult Second(string areaName) 
{ 
    return new ContentResult() { Content = "Second : " + areaName}; 
} 
+0

你是對的!它適用於哪個areaName,但有沒有辦法將區域作爲參數?假設我們有Route(「{area}/Second」)以及如何重定向到不同的區域? – us3r 2015-02-05 14:07:11

+0

同意。在路由站點中的任何區域時,ASP.NET MVC使用「{area}」。因此建議將該參數重命名以避免混淆。 – 2015-02-05 14:07:34