我有以下行动
public ActionResult TemplateBuilder(int id, int? processId) { }
然后我有以下
@Url.Action("TemplateBuilder","InspectionTemplate")/id/processId
然后,网址如下:InspectionTemplate/TemplateBuilder/1/2
但如果我使用
return RedirectToAction("TemplateBuilder","InspectionTemplate", new { id=1, processId = 2});
然后我得到以下结果:InspectionTemplate/TemplateBuilder/1?processId=2
我该如何解决这个问题。
这是我的路由
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "IDRoute",
url: "{controller}/{action}/{id}",
defaults: new
{
controller = "Login",
action = "Index",
id = UrlParameter.Optional
}
);
routes.MapRoute(
name: "ProcessRoute",
url: "{controller}/{action}/{id}/{processId}",
defaults: new
{
controller = "InspectionTemplate",
action = "TemplateBuilder",
id = UrlParameter.Optional,
processId = UrlParameter.Optional
}
);
routes.MapRoute(
name: "DateRoute",
url: "{controller}/{action}/{year}/{month}/{day}",
defaults: new
{
controller = "Inspection",
action = "Assign",
year = UrlParameter.Optional,
month = UrlParameter.Optional,
day = UrlParameter.Optional
}
);
}
答案 0 :(得分:1)
您的路线定义存在3个问题
UrlParameter.Optional
。如果你只提供一个,那么
路由引擎无法知道应该绑定哪个参数
所以值将作为查询字符串参数添加。)IDRoute
将匹配,因此需要交换路由。您的定义应该是
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "ProcessRoute",
url: "InspectionTemplate/{action}/{id}/{processId}",
defaults: new { controller = "InspectionTemplate", action = "TemplateBuilder", processId = UrlParameter.Optional }
);
routes.MapRoute(
name: "DateRoute",
url: "Inspection/{action}/{year}/{month}/{day}",
defaults: new { controller = "Inspection", action = "Assign", } // assumes all parameters are required
);
// The default will only be match if the url does not start with '/InspectionTemplate/' or '/Inspection/'
routes.MapRoute(
name: "IDRoute",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);
}