I have created a WEB API
in visual studio 2015
using MySQL
DB. I have three GET
methods. One is which gives all the data, second one is which gives the result based on ID
and 3rd one is which should gives result on basis of a serial number. First two methods are working perfect but the last one is not. I have also added multiple Routes in the solution. Below is my code
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]+$" }
Controller
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
is the one which is not working. I have tried to change the routes but still the result is same. The solution 1 and solution 2 given are not working for me.
Any help would be highly appreciated.