如果我在行动中设置路线属性,如下所述。
public class AccountController : Controller
{
[Route("Login")]
public ActionResult Login()
{
}
}
我的默认路由不起作用/Account/Login
我希望两个网址都能正常工作
答案 0 :(得分:0)
好的,现在我得到了你想要的东西。在RouteConfig.cs
( App_Start 文件夹)中,您有:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
您应该添加另一条路线 AFTER 默认值:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
//this is your additional route
routes.MapRoute(
name: "Login",
url: "Login",
defaults: new { controller = "Account", action = "Login"}
);
}
删除Route
方法之上的Login
属性。现在,您可以使用两个网址调用您的方法。
替代。像这样改变你的控制器:
public class AccountController : Controller
{
public ActionResult Login()
{
return Login();
}
[Route("Login")]
public ActionResult Login2()
{
return Login();
}
}