My route config looks like this
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
I have my user controller divided by action into separate files, so all GET
operations are in the GetUserController.cs
file, PUT
operations in PutUserController.cs
and so on...
The GET
file has a partial class like this
[RoutePrefix("api/users/{userId:Guid}/locations")]
public partial class UsersController : MyCustomApiController
{
[Route("{locationId:Guid}/list")]
[HttpPost]
public async Task<IHttpActionResult> GetUsers(Guid userId, Guid locationId, [FromBody] Contracts.UserRequest request)
{
}
}
The PUT
file has a partial class like this
public partial class UsersController : MyCustomApiController
{
[Route("{locationId:Guid}/insert")]
[HttpPut]
public async Task<IHttpActionResult> InsertUser(Guid userId, Guid locationId, [FromBody] Contracts.UserRequest request)
{
}
}
No matter what I do, I always get a 404 Error. I am testing with Postman using Content-Type
as application/json
The URL I am using is
http://localhost:52450/api/users/3F3E0740-1BCB-413A-93E9-4C9290CB2C22/locations/4F3E0740-1BCB-413A-93E9-4C9290CB2C22/list
with a POST
since I couldn't use GET
to post a complex type for the first method
and
http://localhost:52450/api/users/3F3E0740-1BCB-413A-93E9-4C9290CB2C22/locations/4F3E0740-1BCB-413A-93E9-4C9290CB2C22/insert
with a PUT
What other routes do I need to setup in the route config if at all?
EDIT
Strangely another controller which is also a partial class seems to work with the same configuration
[RoutePrefix("api/products")]
public partial class ProductController : MyCustomApiController
{
[Route("insert")]
[HttpPut]
public async Task<IHttpActionResult> InsertProduct([FromBody] InsertProductRequest request)
{
}
}
This is the global.asax.cs that wires up everything.
protected void Application_Start(object sender, EventArgs e)
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
FilterConfig.RegisterFilters(GlobalFilters.Filters);
// Only allow Tls1.2!
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
}