简体   繁体   中英

Http 405: The requested resource does not support 'GET'

Hello i have a register method that am trying to use which is infact a POST but returns a Get. This happens on the production server.

   [RoutePrefix("register')]
   public class RegisterController : ApiController
   {
     private UserRepository userRepository;

    public RegisterController()
    {
        userRepository = new UserRepository();
    }
    [AllowAnonymous]
    [Route("")]
    public async Task<IHttpActionResult> Register(Users user)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        IdentityResult result = await userRepository.RegisterUser(user);
        IHttpActionResult error = GetError(result);

        if(error != null)
        {
            return error;
        }

        return Ok();
    }

Tried this solution https://docs.microsoft.com/en-us/aspnet/web-api/overview/testing-and-debugging/troubleshooting-http-405-errors-after-publishing-web-api-applications with no success

You have to update your Register method to allow Post calls.

See below.

[AllowAnonymous]
[Route("register")]
[HttpPost]
public async Task<IHttpActionResult> Register(Users user)
{
   //Your code
}

I encountered this problem before (405 : Method not allowed) and removing WebDav publish features resolved this. You can go to Add/Remove features and under IIS, try removing Web Dav publishing. Below screen shot taken from windows 10 console (it might vary depending upon server OS)

And yes, I believe [HttpPost] annotation is required.

在此处输入图片说明

In your controller you will have two register methods, one with get annotation to return the view and second with post annotation to create or register the user itself. I have created a new project and I can both as follows

 [AllowAnonymous]
        public ActionResult Register()
        {
            return View();
        }

        //
        // POST: /Account/Register
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
            // a lot goes in here
            }
            return View(model);
        }

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