yjkim
yjkim

Reputation: 13

URL route in ASP.NET MVC4 Web API

It is my first time Web programming in ASP.NET. I'm trying to setting URL route on WebApiConfig.cs each method. but methods with the same parameter type is show a 404 error According to the order of registers in WebApiConfig.cs. This problem occurs even though the action name is set above the method. Why can't the url path be found depending on the location of the register function? I am also curious about the difference between WebApiConfig.cs and RouteConfig.cs. Thank you for reading the long article.

Upvotes: 1

Views: 40

Answers (1)

tmaj
tmaj

Reputation: 34957

You don't need all the specific routes in WebApiConfig.

You could leave it as the default:

config.Routes.MapHttpRoute(
   name: "api",
   routeTemplate: "api/{controller}/{action}/{id}",
   defaults: new { id = RouteParameter.Optional }
);

This should make Post() and RoadDept() work.

For the other methods e.g. public HttpResponseMessage SelectData(HttpRequestMessage manage_no) you should not be accepting HttpRequestMessage as an argument.

You should be accepting the values (or a dto).

Here's an example from Attribute Routing in ASP.NET Web API 2:

[Route("customers/{customerId}/orders")]
public IEnumerable<Order> GetOrdersByCustomer(int customerId) { ... }

Here's another example:

[HttpPost]
[Route("products")]
public IActionResult Action3([FromBody] Product product)
{
   // Validate product

   var id = product.Id;
   ...
}

Also, your shown methods are POST, not get, I would recommend trying with a GET method that doesn't accept any parameters and confirm it works first.

Upvotes: 1

Related Questions