ASP.NET MVC 路由参数
asp.net mvc 路由模式是
{"some_parameter/{controller}/{action}/{id}"}
如果 some_parameter
可以为 null 或字符串为空,这是否是有效格式
asp.net mvc routing pattern is
{"some_parameter/{controller}/{action}/{id}"}
Is this a valid format if some_parameter
can be null or string empty
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您想要的是
{some_parameter}/{controller}/{action}/{id}
(注意“some_parameter”周围的大括号),在这种情况下它不应该为 null 或空, 我认为。如果some_parameter
为空,您认为您的最终 URL 会如何与路由匹配? “mysite.com//mycontroller/myaction/myid”?路由引擎只匹配模式。如果您想同时处理
{some_parameter}/{controller}/{action}/{id}
和{controller}/{action}/{id}
,只需定义两条路线。I believe that what you wanted is
{some_parameter}/{controller}/{action}/{id}
(notice curly brackets around "some_parameter") and in that case it shouldn't be null or empty, I think. How do you think your end URL might look like to match the route in case whensome_parameter
is empty? "mysite.com//mycontroller/myaction/myid"?Routing engine just matches patterns. If you want to handle both
{some_parameter}/{controller}/{action}/{id}
and{controller}/{action}/{id}
, just define both routes.编辑
我刚刚重新排序了路线注册,以便它可以正常工作:
它们应该按该顺序注册。另外,第二条路由需要 id 和 some_parameter 参数,否则它永远不会因为前面的路由而被命中。即使 some_parameter 和 id 参数设置为可选,但这永远不会发生,因为如果它为空,之前的路由会捕获它。
Edit
I've just reordered the route registration so that it would work:
They should be registered in that order. Additionally the second route requires an id and some_parameter parameter otherwise it will never be hit because of the route before it. Even though the some_parameter and id parameters are set to optional, that would never happen because the route before would catch it if it was empty.