以前fw mvc中,有如下的路由配置
context.MapRoute(
"Mobile_default",
"m-{platform}/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
当url 访问 xxx站点/m-Wap的时候,会自动匹配解析到配置中的路由,并且 routedata["platform"]会得到 Wap 的参数,那么迁移到net core之后,这个路由配置需要怎样去写呢?
PS:controller 是 Area/Mobile 下边的,官方的文档area的配置是
routes.MapRoute(
name: "area",
template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
试试
routes.MapRoute(
"Mobile_default",
"m-{platform}/{controller}/{action}/{id}",
new { area= "Mobile", controller = "Home", action = "Index" });
app.UseMvc(routes => { routes.MapRoute( "Mobile_default", "m-{platform}/{controller}/{action}/{id}", new { area = "Mobile", controller = "Home", action = "Index" }); routes.MapRoute( name: "area", template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"); routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); });
然后 访问本地调试 站点 http://localhost:60590/m-Wap ,
会报错:找不到与以下网址对应的网页:http://localhost:60590/m-Wap
这个还是路由配置的问题吧?
@死神的背影: 之前漏了问号,id需要加上问号
routes.MapRoute(
"Mobile_default",
"m-{platform}/{controller}/{action}/{id?}",
new { area= "Mobile", controller = "Home", action = "Index" });
@死神的背影: 路由实测没问题,另外,可以通过RouteData.Values["platform"]
获取路由参数值
@dudu: OK,已经测试过,没有问题,多谢