ASP.NET MVC 3可选参数路由问题 [英] ASP.NET MVC 3 Optional Parameter Routing Issue

查看:141
本文介绍了ASP.NET MVC 3可选参数路由问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<一个href=\"http://blogs.msdn.com/b/simonince/archive/2011/02/02/asp-net-mvc-3-optional-parameter-routing-issue.aspx\" rel=\"nofollow\">http://blogs.msdn.com/b/simonince/archive/2011/02/02/asp-net-mvc-3-optional-parameter-routing-issue.aspx

在上述网站中提到的解决方法是可以接受的。但如果最后一个参数是不是可选的,前两个是在MVC3可选会发生什么?任何人都可以知道解决方法。它只是一个疑问,这是困惑我。

The workaround mentioned in the above site is acceptable. But what will happen if the last parameter is not optional and the first two are optional in MVC3?. Can anybody know the workaround. Its just a doubt which is confusing me.

推荐答案

在一个MVC3路由定义,只有最后一个参数可以是可选的。正如纳特暗示,可以为同一个控制器的操作方法多条路线。

In an MVC3 route definition, only the last parameter can be optional. As Nat hints at, you can create multiple routes for the same controller action method.

如果你想有一个必需的参数和2个可选参数,你可以定义多个路线:

If you want to have one required parameter and 2 optional parameters, you can define multiple routes:

...MapRoute(null, "static-segment/{required}/{optional1}/{optional2}", 
    new { controller = "ControllerName", action = "ActionName", 
        optional2 = UrlParameter.Optional });

...MapRoute(null, "static-segment/{required}/{optional1}", 
    new { controller = "ControllerName", action = "ActionName", 
        optional1 = UrlParameter.Optional });

...MapRoute(null, "static-segment/{required}/{optional2}", 
    new { controller = "ControllerName", action = "ActionName", 
        optional2 = UrlParameter.Optional });

有那里有2个可选参数单一路线是你不能做MVC3。此外,有一个可选的参数进来的路线所需的参数之前的东西,你不能做MVC3。你需要充实所有的路由模式方案,并创建将在您的网址模式中的每个大小写相匹配的路由。

Having a single route where there are 2 optional parameters is something you can't do in MVC3. Also, having an optional parameter come before a required parameter in a route is something you can't do in MVC3. You need to flesh out all of the routing pattern scenarios and create routes that will match each case in your URL schema.

这篇关于ASP.NET MVC 3可选参数路由问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆