0
嘗試使用RedirectToAction()
時出現問題。RedirectToAction()(MVC5)的意外行爲
在我RouteConfig.cs
我有這樣的事情:
routes.MapRoute(
name: "Custom",
url: "myroot/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = "0" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = "0" }
);
從我的控制器,如果我試試這個:
public ActionResult Contact()
{
return RedirectToAction("About", "Home");
}
我得到這個網址:
http://localhost:51547/myroot/Home/About
哪個不是我所期望的。如果我試試這個:
return RedirectToRoute("Default", new { controller = "Home", action = "About" });
我得到這個網址:
http://localhost:51547/Home/About
這對我來說很有意義。
任何人都可以解釋爲什麼RedirectToAction
將myroot/
前綴添加到URL而不是匹配Default
路由嗎?
謝謝!
看起來你已經在具有該前綴 –
您好斯蒂芬的方法,謝謝你你的迴應。實際上,我在默認的MVC應用程序中測試了這個。我的方法是這樣的: public ActionResult Contact() { return RedirectToAction(「About」,「Home」); } 當我寫這個URL(http:// localhost:12345/Home/Contact)時,它會轉到「關於」視圖,將myroot /前綴寫入url,如上所述。 – AjStar08
你的方法是什麼? (你沒有顯示任何) –