[英]ASP.NET MVC Routing - Redirect to Route when Action Name and Url is same
我需要根據角色重定向不同的操作。 我對RouteConfig.cs進行了以下更改。
RouteConfig.cs
routes.MapRoute(
name: "borrower",
url: "borrower",
defaults: new { controller = "Home", action = "Borrower" });
routes.MapRoute(
name: "broker",
url: "broker",
defaults: new { controller = "Home", action = "Broker" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
這是我的控制器
HomeController.cs
public class HomeController : Controller
{
public ActionResult Index()
{
if (UserPrincipal.Current.Identity.IsAuthenticated)
{
if (UserPrincipal.Current.IsInRole("Broker"))
{
return RedirectToRoute("broker");
}
else if (UserPrincipal.Current.IsInRole("Borrower"))
{
return RedirectToRoute("borrower");
}
}
return View();
}
public ActionResult Broker()
{
return View();
}
public ActionResult Borrower()
{
return View();
}
}
代碼流正常工作直到return RedirectToRoute("")
但在此代碼流之后永遠不會進入相關/適當的操作並返回空視圖。
注意: Broker和Borrower視圖都不為空,它們具有靜態文本。
在網絡選項卡中,您可以看到正在執行正確的重定向,並且瀏覽器中的URL正在設置為路由URL。
RedirectToRoute("Broker")
導致302
響應並且用戶被重定向到abc.com/broker
,但是abc.com/broker
沒有在HomeController
點擊Broker
action方法並返回空視圖。 根據RouteConfig.cs
它應該擊中Broker
動作方法。
請指出我在這里做錯了什么。
事實證明,我在Web項目的根目錄下有一個名為“Borker”和“Borrower”的目錄。 由於此MVC路由模塊被忽略,請求( abc.com/broker
和abc.com/borrower
)被移動到這些目錄。 所以我重命名了這些目錄,一切按預期工作:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.