Asp.net 具有相同url的多个路由
目前,我的RouteConfig.cs中有以下内容:Asp.net 具有相同url的多个路由,asp.net,asp.net-mvc,asp.net-mvc-4,Asp.net,Asp.net Mvc,Asp.net Mvc 4,目前,我的RouteConfig.cs中有以下内容: routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); ro
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Identity",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Profile", action = "Identity" }
);
我尝试在动作方法中使用以下内容:
return RedirectToRoute("Identity", new { @id = id });
但它似乎没有到达那里,当我与Fiddler核对时,我看到请求返回到我当前所在的页面,它似乎到达了默认路径。无论如何,是否有强制它获取标识1的方法,即使它们的URL相同,我想在需要时使用该方法强制用户访问另一个控制器。MVC将在找到匹配的路由后立即停止查找路由。在您的情况下,它将按以下顺序显示:1.)默认2.)标识 如果要创建具有相同模式的特定管线,可以使用以下代码实现:
routes.MapRoute(
name: "Identity",
url: "Profile/{action}/{id}",
defaults: new { controller = "Profile", action = "Identity" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
一旦找到匹配的路线,MVC将停止寻找路线。在您的情况下,它将按以下顺序显示:1.)默认2.)标识 如果要创建具有相同模式的特定管线,可以使用以下代码实现:
routes.MapRoute(
name: "Identity",
url: "Profile/{action}/{id}",
defaults: new { controller = "Profile", action = "Identity" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
您不需要将用户“强制”到其他控制器。使用RedirectToAction方法是一种很好的做法,这样您就可以提供您想要带给用户的控制器和操作 @thiag0提供的解决方案将不起作用 使用以下命令
return RedirectToAction("Identity", "Profile", new { id = 5 });
在配置文件控制器中,确保可以接受该参数
public ActionResult Identity(int id)
{
return View();
}
在RouteConfig.cs中
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Identity",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Profile", action = "Identity"}
);
您不需要将用户“强制”到其他控制器。使用RedirectToAction方法是一种很好的做法,这样您就可以提供您想要带给用户的控制器和操作 @thiag0提供的解决方案将不起作用 使用以下命令
return RedirectToAction("Identity", "Profile", new { id = 5 });
在配置文件控制器中,确保可以接受该参数
public ActionResult Identity(int id)
{
return View();
}
在RouteConfig.cs中
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Identity",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Profile", action = "Identity"}
);
把标识放在默认的前面?正如@zgood所说,我的理解是默认的路由配置应该总是在路由配置的末尾。由于路由配置将解析匹配的配置,因此它将接受并路由到该路径。因此,如果在开始时指定默认配置,它将始终匹配并路由到该路径。将标识一置于默认配置之前?正如@zgood所说,我的理解是,默认路由配置应始终位于路由配置的末尾。由于路由配置将解析匹配的配置,因此它将接受并路由到该路径。因此,如果在开始时指定默认配置,它将始终匹配并路由到该路径。