Web.API MapHttpRoute参数 [英] Web.API MapHttpRoute parameters

查看:608
本文介绍了Web.API MapHttpRoute参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的Web.API路由问题。我有以下两种途径:

I'm having problems with my Web.API routing. I have the following two routes:

config.Routes.MapHttpRoute(
    name: "MethodOne",
    routeTemplate: "api/{controller}/{action}/{id}/{type}",
    defaults: new { id = RouteParameter.Optional, type = RouteParameter.Optional }
);

config.Routes.MapHttpRoute(
    name: "MethodTwo",
    routeTemplate: "api/{controller}/{action}/{directory}/{report}",
    defaults: new { directory = RouteParameter.Optional, report = RouteParameter.Optional }
);

这两种方法在我的控制器:

And in my controller these two methods:

[HttpGet]
[ActionName("methodone")]
public string MethodOne(string id, string type)
{
    return string.Empty;
}

[HttpGet]
[ActionName("methodtwo")]
public string MethodTwo(string directory, string report)
{
    return string.Empty;
}

这两个看似无法比邻而居。如果我注释掉WebApiConfig的MethodOne路线,MethodTwo路线的作品。在谈到MethodTwo路线可以让MethodOne工作。离开这两个注释掉,MethodOne工作,但不是MethodTwo。

These two seemingly cannot live side by side. If I comment out the MethodOne route in WebApiConfig, the MethodTwo route works. Commenting MethodTwo route allows MethodOne to work. Leaving both uncommented, MethodOne works, but not MethodTwo.

我希望用一条路线为这些话,好像他们就必须有相同的参数名。谁写与泛型参数名称的方法?坏。我真的不希望我的方法有相同的参数名称(如P1,P2,P3),所以我想我可能只是为新的方法来创建一个路由。但是,即使这似乎并没有工作。

I was hoping to use one route for both of these then it seems they would have to have the same parameter names. Who writes methods with generic parameter names? Bad. I really don't want my methods to have the same parameter names (like p1, p2, p3), so I thought I could create a route just for the new method. But even this doesn't seem to work.

我真的很怀念 WebGet(UriTemplate =)从WCF休息。

I really miss the WebGet(UriTemplate="") from WCF rest.

我有一个控制器,有许多方法,一些带有1,2,3或甚至更多的参数。我不能相信我不能使用有意义的参数名称与MapHttpRoute方法。

I have one controller that has many methods, some with 1, 2, 3 or even more parameters. I can't believe I cant use meaningful parameter names with the MapHttpRoute approach.

我可以完全注释的东西出来,并使用 WebGet() ...但在此之前我到了那里我想看看,如果我失去了一些东西。

I could comment that stuff out entirely and use WebGet() … but before I got there I wanted to see if I'm missing something.

推荐答案

您看到了这个问题的原因是因为你的第一条路线将同时匹配请求。 URL中的ID和类型的令牌将同时匹配请求,因为正在运行的路由时,它会尝试解析URL和匹配您的网址每个段。

The reason you are seeing this problem is because your first route will match both requests. The id and type token in the URL will match both requests because when the route is being run, it will try parse the URL and match each segment against your URL.

所以,你的第一条路线会很乐意如下同时匹配的请求。

So your first route will happily match both requests as follows.

~/methodone/1/mytype => action = methodone, id = 1, and type = mytype
~/methodtwo/dictioanry/report => action = methodtwo, id = dictionary, and type = report

要解决这个问题,你应该使用类似的路线

To work around this, you should use route like

config.Routes.MapHttpRoute(
    name: "MethodOne",
    routeTemplate: "api/{controller}/methodone/{id}/{type}",
    defaults: new { id = RouteParameter.Optional, type = RouteParameter.Optional }
);

config.Routes.MapHttpRoute(
    name: "MethodTwo",
    routeTemplate: "api/{controller}/methodtwo/{directory}/{report}",
    defaults: new { directory = RouteParameter.Optional, report = RouteParameter.Optional }
);

即使你使用WebGet,你可能需要做一些类似disambiguous这两个方法,我相信。

Even if you use WebGet, you might need to do something similarly to disambiguous those two methods I believe.

这篇关于Web.API MapHttpRoute参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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