Search code examples
c#asp.net-coremiddleware

ASP.NET Core Middleware


I have build custom Identity Middle-ware. Inside Invoke method I am checking if request has token etc. After token is checked I want to pass request further to controller. It`s working for GET request - it jump into controller method. It is not working for POST request.

Here is Invoke Method

public async Task Invoke(HttpContext context)
{
    //checking
    await _next(context);
}

Its working controller:

[Route("api/[controller]")]
public class ValuesController : Controller
{
    // GET api/values
    [AllowAnonymous]
    [HttpGet]
    public string Get()
    {
        return "Allow annymous";
    }
 }

And not working one

[Route("api/[controller]")]
public class AccountController : Controller
{
    [AllowAnonymous]
    [HttpPost]
    public void Login()
    {
         //some logic
          HttpContext.Response.WriteAsync("Unauthorized");
    }
}

Making POST call Postman returns 404 not found.


Solution

  • The route for the action in question would be /api/Account. Make sure that is correct.

    If instead you wanted /api/Account/Login:

    [Route("api/[controller]/[action]")]
    public class AccountController : Controller
    {
        [AllowAnonymous]
        [HttpPost]
        public void Login()
        {
             //some logic
              HttpContext.Response.WriteAsync("Unauthorized");
        }
    }