2015-09-05 264 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

我已經使用屏幕截圖更新了我的答案。 –