Asp.net mvc 3 MVC路由未被调用
我已经登记了这条路线:Asp.net mvc 3 MVC路由未被调用,asp.net-mvc-3,Asp.net Mvc 3,我已经登记了这条路线: context.MapRoute( "Manager", "manage/{id}/{action}", new { action = "index", controller = "manage", id = UrlParameter.Optional }, new string[] { "Web.Areas.Books.Controllers"
context.MapRoute(
"Manager",
"manage/{id}/{action}",
new { action = "index", controller = "manage", id = UrlParameter.Optional },
new string[] { "Web.Areas.Books.Controllers" }
);
然后我有以下两个URL:
http://<site>/manage <-- hits the index action of managecontroller
http://<site>/manage/publish <-- ALSO HITS INDEX VIEW even I have publish action
http:///manage 您希望第二段绑定到操作参数,但在路由中它是id参数
"manage/{id}/{action}"
使用/manage/publish
URL,id参数的值将为publish
框架找不到操作参数,因此它使用默认值并将其重定向到索引操作。您只能将末尾的参数作为可选参数
如果必须在中间指定和整数ID,可以通过定义约束来实现它。
context.MapRoute(
"Manager",
"manage/{id}/{action}",
new { action = "index", controller = "manage" },
new { id = @"\d+" }, //second segment has to be an integer
new string[] { "Web.Areas.Books.Controllers" }
);
其他URL应返回默认路由并正常工作。@Projapati您需要为所需内容创建自定义视图引擎。@Projapati尝试我发布的路由。我最终将路由从/{id}/{action}更改为{action}/{id}
context.MapRoute(
"Manager",
"manage/{id}/{action}",
new { action = "index", controller = "manage" },
new { id = @"\d+" }, //second segment has to be an integer
new string[] { "Web.Areas.Books.Controllers" }
);