mvc 4中的路由问题 [英] Problem in routing in mvc 4

查看:91
本文介绍了mvc 4中的路由问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Global.asax中有两个路由。两者都是正确的,但问题是只有路线运行,例如,如果我的网址是api / catagory / GetAllCatagory,那么它运行正常,但如果我的网址是api / catagory / Id那么它会给出错误。





I have tow Routes in Global.asax. Both are correct but the problem is that only route run which is first e.g if my url us api/catagory/GetAllCatagory then it run fine but if my url is api/catagory/Id then it gives error.


routes.MapHttpRoute(
              name: "ActionApi",
              routeTemplate: "api/{controller}/{action}/{id}",
              defaults: new { id = RouteParameter.Optional });




          routes.MapHttpRoute(
               name: "DefaultApi",
               routeTemplate: "api/{controller}/{id}",
               defaults: new { id = RouteParameter.Optional });

推荐答案

不是路由专家,但由于您的ID是可选的,因此可能如下。

假设您发送api / category / id



读取第一条路线,它与你的api /控制器/动作相匹配(因为你有三个部分,计算机不区分名称的含义) - 也就是说,第一条路线匹配为如果它是没有参数的动作请求。



因此,您的路线会尝试将id从api / category / id映射到api / {controller} / {action}中的action



尝试在第一个ro中设置id ute as mandatory。





如果这有帮助,请花些时间接受解决方案。谢谢。
Not an expert on routing, but since your id is optional it may be the following.
Let's say you send api/category/id

First route is read and it matches your api/controller/action (since you have three parts and computer does not distinguish the names for meaning) - that is, first route matches as if it is action request without parameters.

Therefore, your route tries to map id from api/category/id to action in api/{controller}/{action}

Try setting id in first route as mandatory.


If this helps, please take time to accept the solution. Thank you.


这篇关于mvc 4中的路由问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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