我想在我的MVC应用程序中实现自定义路由,但我无法让它工作。 我想在创建MVC应用程序时保留指定的存在默认路由。
我想要有效的路线应如下所示:
默认值:/ {controller} / {action} / {id}
新自定义:/ {controller} / {appid} / {action} / {id}
使用自定义域,我将在每个请求中传递appid,但{id}应该是可选的。 因此,路线定义如下:
routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Updates", action = "Index", id = UrlParameter.Optional } ); routes.MapRoute( name: "NewPackageRoute", url: "{controller}/{appid}/{action}/{id}", defaults: new { controller = "apps", appid = "00000000000000000000", action = "Index", id = UrlParameter.Optional } );在AppsController上,我有一个操作方法索引:
public ActionResult Index(string appid, string id)如果我提供id参数,则按预期命中此操作方法。 我期望如果没有提供{id}参数也会调用此方法,从而将id参数保留为null。 然而,这不会发生。
我该如何定义不同的路线? 我是否应该使用AttributeRouting来实现我的目标?
我也许应该添加...如果我在Apps控制器上调用搜索操作,我就可以开始操作了。 这将通过默认路线发生......
希望我有足够的信息......
I want to implement a custom route in my MVC app and I just can't get it to work. I want to keep the exist default route as specified when you create your MVC app.
The routes I want to be valid should look like this:
default: /{controller}/{action}/{id}
new custom: /{controller}/{appid}/{action}/{id}
With the custom domain, I will be passing the appid in with every request, but the {id} should be optional. The routes are thus defined as follow:
routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Updates", action = "Index", id = UrlParameter.Optional } ); routes.MapRoute( name: "NewPackageRoute", url: "{controller}/{appid}/{action}/{id}", defaults: new { controller = "apps", appid = "00000000000000000000", action = "Index", id = UrlParameter.Optional } );On the AppsController, I have an action method Index:
public ActionResult Index(string appid, string id)If I supply the id parameter, this action method is hit as expected. I am expecting this method to also be called if the {id} parameter is not supplied, thus leaving the id parameter as null. This however does not happen.
How should I define my route differently? Should I perhaps rather make use of AttributeRouting for achieve my goal?
I should maybe also add... If I call the Search action on the Apps controller, I can get to the action. This would happen through the default route...
Hope I have all and enough info...
最满意答案
哦,我的,但我想我应该在发布之前尝试过。 我把这个问题留了一天,现在我没有任何努力就让它工作了......
谢谢@StephenMuecke。 你确实指出了我忘记的路线的顺序。 我最初玩过这个订单,但是那时我在路线定义中遇到了其他问题导致它无法正常工作。
我添加的所有内容都是对appid路由值的长度检查,它正在工作......我的路由定义如下:
routes.MapRoute( name: "NewPackageRoute", url: "apps/{appid}/{action}/{id}", defaults: new { controller = "Apps", action = "Index", id = UrlParameter.Optional }, constraints: new { appid = @"^[a-zA-Z0-9]{20}" } ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Updates", action = "Index", id = UrlParameter.Optional } );Oh my, but I guess I should've tried before I posted this. I left this issue for a day and now I got it working without any effort...
Thanks @StephenMuecke. You did point out the ordering of the routes which I forgot about. I played with the order initially, but at that point I had other issues in the route definitions that caused it not to work.
All I added was as length check on the appid route value and it is working... My routes are defined as follow now:
routes.MapRoute( name: "NewPackageRoute", url: "apps/{appid}/{action}/{id}", defaults: new { controller = "Apps", action = "Index", id = UrlParameter.Optional }, constraints: new { appid = @"^[a-zA-Z0-9]{20}" } ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Updates", action = "Index", id = UrlParameter.Optional } );更多推荐
发布评论