MVC附加自定义路由(MVC Additional Custom routes)

编程入门 行业动态 更新时间:2024-10-28 03:25:38
MVC附加自定义路由(MVC Additional Custom routes)

我想在我的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 } );

更多推荐

本文发布于:2023-07-05 00:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1031217.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   路由   MVC   routes   Custom

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!