0
重載Get方法路由問題,我有產品控制器2種Get方法如下:用的WebAPI
public IHttpActionResult Get(string keywordSearch)
[Route("api/Product/{id:long}")]
public IHttpActionResult Get(long id)
以下網址的工作原理:
http://localhost:61577/api/Product?keywordSearch=test
http://localhost:61577/api/Product/1
這一次失敗,與消息:
http://localhost:61577/api/Product/test
{
"Message": "No HTTP resource was found that matches the request URI 'http://localhost:61577/api/Product/test'.",
"MessageDetail": "No action was found on the controller 'Product' that matches the request."
}
WebApiConfig.cs具有以下配置:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
請指教,我應該在API函數或配置中做什麼修改才能使其工作。
**您的解決方案對我有幫助**我在代碼中做了以下更改並且它變得功能正常。 1.增加RoutePrefix '[RoutePrefix( 「API /產物」)]'' 公共類ProductController的:ApiController' 2.添加的路由 '〔路線( 「{keywordSearch}」)]'' 公共IHttpActionResult獲取(string keywordSearch)' – AaBa
將以第二種方式更新答案 –