I might have misunderstood the concept of MapHttpRoute entirely. I'm developing an asp.net web api, having hard time getting the Routing to work.
I have a MyUserController.cs and MyTaskController.cs. MyUserController accepts username/password and returns a sessionkey, it works as expected.
But MyTaskController keeps complaining when I pass the second parameter. http://localhost:59720/api/mytask/somesessionkey - works
http://localhost:59720/api/mytask/somesessionkey/1 - doesn't work, throws the following error
{"Message":"The requested resource does not support http method 'GET'."}
I tried with and without [HttpGet] on the methods.
MyTaskController.cs
//works fine
public IEnumerable<MyTask> Get(string sessionkey)
{
MyTaskModel mtm = new MyTaskModel();
return mtm.GetAll();
}
//doesn't work
public string Get(string sessionkey, int id)
{
if(isValid(sessionkey))
{
return DataAccess().GetTask(id);
}
return "";
}
MyUserController.cs
public string Get(string username, string password)
{
string sessionkey = "tempsession";
return sessionkey;
}
WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "MyUserApi",
routeTemplate: "api/{controller}/{username}/{password}",
defaults: new { controller = "MyUser" }
);
config.Routes.MapHttpRoute(
name: "MyTaskApi",
routeTemplate: "api/Mytask/{sessionkey}/{id}",
defaults: new
{
controller = "MyTask",
id= RouteParameter.Optional
}
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"));
}