I have two methods for getting a list of projects, or an individual project. However, when I try to implement attribute routing I get a '405 Method Not Allowed' error. One method takes a string (returning a list of projects), the other an integer (returning a single project), how can I get the routing to work?
[HttpGet]
[Route("api/projects/{search}")]
public List<JsonProject> Get(string search = null)
{
}
[HttpGet]
[Route("api/projects/{id:int}")]
public JsonProject Get(int id)
{
}
The 'search' parameter is optional (by default it returns all records) and I may want to add 'sort' as well (also optional). If I take Route out I can get the list of projects, but not the individual project
global.asax has
GlobalConfiguration.Configure(WebApiConfig.Register);
and the routing
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
}
}
Allowheader in your405 Method Not Allowedresponse and let us know what it tells