locked
Requested Resource Does Not Support HTTP Get RRS feed

  • Question

  • User1928065626 posted

    I am trying to implement OAuth2 in my Web API 2 by following the tutorial below:

    http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/

    This tutorial is quite old so any questions posted to the discussion section of this tutorial goes unanswered. The tutorial mentioned above requires VS 2013 but I have VS 2012. The author of the tutorial said people with 2012 can follow along if they download and install Web Tools 2013.1 for VS 2012.

    I did all that but I'm stuck on step 7 where it says I can call the Register method of the AccountController controller in the browser to register a user and then Entity Framework will create a database to store users' info.  The problem is I'm getting the error message 

    <Error>
     <Message>
      The requested resource does not support http method 'GET'.
     </Message>
    </Error>

    Below is the code for AccountController. Please take a look to determine what I might be missing, thanks.

    [RoutePrefix("api/Account")]
        public class AccountController : ApiController
        {
            private AuthRepository _repo = null;
     
            public AccountController()
            {
                _repo = new AuthRepository();
            }
     
            // POST api/Account/Register
            [AllowAnonymous]
            [Route("Register")]
            public async Task<IHttpActionResult> Register(UserModel userModel)
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }
     
                IdentityResult result = await _repo.RegisterUser(userModel);
     
                IHttpActionResult errorResult = GetErrorResult(result);
     
                if (errorResult != null)
                {
                    return errorResult;
                }
     
                return Ok();
            }
     
            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    _repo.Dispose();
                }
     
                base.Dispose(disposing);
            }
     
            private IHttpActionResult GetErrorResult(IdentityResult result)
            {
                if (result == null)
                {
                    return InternalServerError();
                }
     
                if (!result.Succeeded)
                {
                    if (result.Errors != null)
                    {
                        foreach (string error in result.Errors)
                        {
                            ModelState.AddModelError("", error);
                        }
                    }
     
                    if (ModelState.IsValid)
                    {
                        // No ModelState errors are available to send, so just return an empty BadRequest.
                        return BadRequest();
                    }
     
                    return BadRequest(ModelState);
                }
     
                return null;
            }
        }
    

    Sunday, March 26, 2017 3:13 AM

All replies

  • User-1618234021 posted

    // POST api/Account/Register [AllowAnonymous] [Route("Register")] public async Task<IHttpActionResult> Register(UserModel userModel) { if (!ModelState.IsValid) { return BadRequest(ModelState); } IdentityResult result = await _repo.RegisterUser(userModel); IHttpActionResult errorResult = GetErrorResult(result); if (errorResult != null) { return errorResult; } return Ok(); }

    Modify the above like the following:

            [AllowAnonymous]
            [Route("Register")]
            [HttpPost]
            public async Task<IHttpActionResult> Register([FromBody] UserModel userModel)
            {
                       //.....
            }

    Then make sure you send a post request to this method with UserModel object added as serialized json.

    Monday, March 27, 2017 4:13 AM
  • User1928065626 posted

    Hi, thanks for your response but your suggestion did not work. I'm still getting the same error.

    Wednesday, March 29, 2017 6:07 AM
  • User-1618234021 posted

    Please share the code you are using to CONSUME this web api method.

    Thursday, March 30, 2017 4:53 AM
  • User-2057865890 posted

    Hi Madjester,

    By default it'll work fine with your code. However there's still another situation which can cause the "not support http method" problem. That is the handler settings in Web.config file or the project and the IIS, or maybe the machine.config file. Please see this article for troubleshoot this problem in the config file: Troubleshooting HTTP 405 Errors after Publishing Web API 2 Applications

    Best Regards,

    Chris

    Thursday, March 30, 2017 5:15 AM