2015-09-05 41 views
1

我有如下一个的WebAPI网址:WebApi URL解析 - 如何获取URL的内联参数?

https://baseaddress/controlapi/api/parameters/Gateway 

参数控制器具有API方法如下,网关是服务参数的参数值:

public HttpResponseMessage Get(string service) 
{ 
} 

GetQueryNameValuePairs()得到的查询字符串来自HttpRequest的键值对。同样有一种方法可以获得内联参数。

在此先感谢。

回答

1

这是关于路由。在WebApi 2中,您可以定义一个匹配URI的路由。

你可以尝试这样的事:需要在你的方法具有相同名称在请求的内容相匹配,以指定

的«参数»的格局。

[Route("controlapi/api/parameters/{parameters}")] 
public HttpResponseMessage Get(string parameters) 
{ 
    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "value"); 
    response.Content = new StringContent(parameters, Encoding.Unicode); 
    return response; 
} 

你可以看到它是如何正确工作的。

Requesting a specific URL through PostMan app with the.

Getting the parameters value (Gateway) in Visual Studio.

+0

的问题是如何从HTTP请求得到的参数值。对不起,如果我不清楚的问题。我正在寻找的是从Uri获取参数的方式,例如https:// baseaddress/controlapi/api/parameters /网关 – SteelBird82

+0

我已经使用屏幕截图更新了我的答案。 –