0

I am getting an exception when I have multiple action methods with same verb i.e. POST, in the Account controller of my newly setup WebApi project using default template in Visual Studio. Here's the info about VS:

Microsoft Visual Studio Enterprise 2019
Version 16.3.10
VisualStudio.16.Release/16.3.10+29519.87

Here is the exception that I get:

InvalidOperationException: The method 'post' on path '/api/Account' is registered multiple times.

Here is my the default routing setup in the Startup file:

 app.UseEndpoints(endpoints => { endpoints.MapControllers(); });

I have also tried the following but it didn't help.

  endpoints.MapControllerRoute(
            name: "api",
            pattern: "{controller}/{id?}");

and

endpoints.MapControllerRoute(
            name: "api",
            pattern: "{controller}/{action}/{id?}");

I have read about the end point routing at the official docs as well, but I am not able to figure out what am I missing. You help is appreciated.

2 Answers 2

2

You need to pass template name i.e. [HttpPost("my-other-route")].

Sign up to request clarification or add additional context in comments.

2 Comments

Is using route attribute necessary for all action methods in ASP.Net Core WebAPI?
@SuperCoder the Route attribute means all HTTP verbs are accepted, and it's typically (in my experience, at least) used on the controller. The Http(Verb) attributes are used on controller actions. If you have multiple actions on the same controller with the same HTTP verb, their route templates have to be unique.
0

Try to change the Route attribute for the Controller as shown

[Route("api/[controller]/[action]")]
[ApiController]
public class StudentsController : ControllerBase
{ ... }

You could call the PostStudent action with the request route like https://localhost:44346/api/students/postStudent and call the PostStudentList action with the request route like https://localhost:44346/api/students/postStudentList.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.