1
這裏是路線定義:如何定義一個單獨的動作來處理來自這個控制器的所有http GET請求?
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{*data}",
defaults: new { data= RouteParameter.Optional }
);
現在,在產品的控制,我有一個這樣定義的操作:
public string Get(string data="")
{
return "value";
}
我測試http-get
使用下列URL
api/product/
api/product/apple
兩者都在工作
但是,如果我用這個URL
api/product/?p=5
它會拋出一個異常
No action was found on the `controller` 'product' that matches the request.
我知道我可以這樣定義的另一個行動來解決問題
public string Get(int p,string data="")
{
return "value";
}
但這對我來說不起作用,因爲url
上的參數是不可預知的,無論是參數的名稱還是參數的值。
所以,我的問題是
有沒有一種方法來定義一個動作來處理所有的http-get
請求是來此控制器不管是什麼樣的查詢字符串在URL中使用?
當你做'api/product?p = 5'時會發生什麼?我不能說我曾經見過一個URL,它的後面跟着一個查詢字符串。 –
仍然是一樣的錯誤 – liuhongbo