0
我使用MySQL
DB在visual studio 2015
中創建了WEB API
。我有三個GET
方法。一個是給出所有數據,第二個是給出基於ID
的結果,第三個是根據序列號給出結果。前兩種方法是完美的,但最後一種不是。我還在解決方案中添加了多個路由。下面是我的代碼無法在單個控制器中運行多個GET方法
WebApiConfig
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "GetByMsn",
routeTemplate: "api/{controller}/{action}/{msn}",
defaults: null,
constraints: new { msn = @"^[a-z]+$" }
控制器
public HttpResponseMessage Get()
{
try
{
return Request.CreateResponse(HttpStatusCode.Found, mEntities.meters_info_dev.ToList());
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
}
}
// GET by ID
public HttpResponseMessage GetByID(int id)
{
try
{
return Request.CreateResponse(HttpStatusCode.Found, mEntities.meters_info_dev.SingleOrDefault(m => m.id == id));
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
}
}
// GET by serial number
public HttpResponseMessage GetByMsn(string msn)
{
try
{
return Request.CreateResponse(HttpStatusCode.Found, mEntities.meters_info_dev.SingleOrDefault(m=> m.meter_msn == msn));
}
catch
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No Data found");
}
}
GetByMsn
是不正常的一個。我試圖改變路線,但結果仍然是一樣的。給出的solution 1和solution 2不適用於我。
任何幫助將不勝感激。