C# RedirectToAction()的意外行为(MVC5)
尝试使用C# RedirectToAction()的意外行为(MVC5),c#,asp.net-mvc-5,redirecttoaction,C#,Asp.net Mvc 5,Redirecttoaction,尝试使用RedirectToAction()时遇到问题 在我的RouteConfig.cs中,我有如下内容: routes.MapRoute( name: "Custom", url: "myroot/{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = "0" } );
RedirectToAction()
时遇到问题
在我的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");
}
return RedirectToRoute("Default", new { controller = "Home", action = "About" });
我得到这个网址:
http://localhost:51547/myroot/Home/About
http://localhost:51547/Home/About
这不是我所期望的。如果我尝试这样做:
public ActionResult Contact()
{
return RedirectToAction("About", "Home");
}
return RedirectToRoute("Default", new { controller = "Home", action = "About" });
我得到这个网址:
http://localhost:51547/myroot/Home/About
http://localhost:51547/Home/About
这对我来说很有意义
有人能解释为什么redirectoaction
将myroot/
前缀添加到URL,而不是匹配Default
路由吗
谢谢大家! 请将您的路线顺序更改为
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = "0" }
);routes.MapRoute(
name: "Custom",
url: "myroot/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = "0" }
);
这可能有用。。。未经测试
return RedirectToAction("action", "controller", new { area = "" });
看起来您已经使用了一个带有前缀的方法,Stephen,谢谢您的回复。实际上,我在默认的MVC应用程序中对此进行了测试。我的方法是:public ActionResult Contact(){return RedirectToAction(“About”,“Home”);}当我编写这个URL()时,它会转到“About”视图,将myroot/前缀写入URL,正如我前面解释的。你的方法是什么?(您没有显示任何内容)