简体   繁体   中英

Multiple HttpPost Method in ASP.NET Core Webb API

I could not head around on my routing rules in ASP.NET Core. I have following Back and Front-end application of user authentication action methods. Since both of these action methods has one parameter with different or additional arguments in payload. Http client could not actually navigate exact method therefore throw an exception with an error "ambiguity" and it is true because both these HttpPost method has one parameter with different arguments. How to make sure that method triggers according to action. Code follows :-

API Controller:-

  [Route("api/[controller]")]
  public class AccountController: Controller
  {
         [HttpPost, ActionName("Login")]
         public async Task<IActionResult> UserLogin(LoginDto user)
         {
            //Code goes here
         }

        [HttpPost, ActionName("Register")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> UserRegistration(RegisterDto register)
        {
             //Code goes here
        }
  }

Frontend API Service

   public async Task<IActionResult> GetLoginAsync(LoginDto loginUser)
   {
                var request = "/api/account";
                var content = new StringContent(JsonConvert.SerializeObject(loginUser), Encoding.UTF8, "application/json");
                try
                {
                    var result = await ExecuteWithResiliencePolicies(() => _client.PostAsync(request, content));
                    return new StatusCodeResult((int)result.StatusCode);

                }
                catch (HttpRequestException)
                {
                    return new StatusCodeResult(StatusCodes.Status503ServiceUnavailable);
                }           
     }

     public async Task<IActionResult> PostRegistrationAsync(RegisterDto registerUser)
     {
                var request = "/api/account";
                var content = new StringContent(JsonConvert.SerializeObject(registerUser), Encoding.UTF8, "application/json");
                try
                {
                    var result = await ExecuteWithResiliencePolicies(() => _client.PostAsync(request, content));
                    return new StatusCodeResult((int)result.StatusCode);

                }
                catch (HttpRequestException)
                {
                    return new StatusCodeResult(StatusCodes.Status503ServiceUnavailable);
                }
      }

Web Controller :-

     [HttpPost, ActionName("Login")]
            [ValidateAntiForgeryToken]
            public async Task<IActionResult> UserLogin(LoginDto user)
            {
                SetCorrelation();
                if (!ModelState.IsValid)
                {
                    return RedirectToAction(nameof(Login));
                }
                await _accountService.GetLoginAsync(user);              
                return RedirectToAction("Index", "Home");
            }

            [HttpPost, ActionName("Register")]
            [ValidateAntiForgeryToken]
            public async Task<IActionResult> UserRegistration(RegisterDto register)
            {
                SetCorrelation();
                if (!ModelState.IsValid)
                {
                    return RedirectToAction(nameof(Login));
                }
                await _accountService.PostRegistrationAsync(register);
                return RedirectToAction("Index", "Home");


}

Web Routing :

app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "WebRoute",
                    template: "{controller}/{action}/{id?}",
                    defaults: new { controller = "Home", action = "Index" },
                    constraints: new { id = "[0-9]+" });                
            });

API Routing:-

app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "KtsWebAPIRoute",
                    template: "{controller=Posts}/{action=GetAsync}/{id?}");

                routes.MapRoute(
                    name: "KtsAccountRoute",
                    template: "{controller}/{action}/{id?}",
                    defaults: new { controller = "Account", Action = "Login" },
                    constraints: new { id = "[0-9]+" });
            });

There are couple of possible solutions. For every solution front-end part should call the following urls

"/api/account/login"
"/api/account/register"

Solution 1

You need to specify template in HttpPost attribute ASP.NET Core routing cannot get action name from request

[Route("api/[controller]")]
public class AccountController : Controller
{
    [HttpPost("Login")]
    public async Task<IActionResult> UserLogin(LoginDto user) { /*..*/ }
}

or you can combine template with ActionName

[Route("api/[controller]")]
public class AccountController : Controller
{
    [HttpPost("[action]"), ActionName("Login")]
    public async Task<IActionResult> UserLogin(LoginDto user) { /*..*/ }
}

Solution 2

Update route for controller to include action name as well

[Route("api/[controller]/[action]")]
public class AccountController : Controller
{
    [HttpPost, ActionName("Login")]
    public async Task<IActionResult> UserLogin(LoginDto user) { /*..*/ }
}

Solution 3

Use Route attributes for actions

[Route("api/[controller]")]
public class AccountController : Controller
{
    [HttpPost, Route("Login")]
    public async Task<IActionResult> UserLogin(LoginDto user) { /*..*/ }
}

Use apiController attribute and ControllerBase

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

and the url should look like this

var uri = "............/api/Account/Login"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM