简体   繁体   English

Http 405: 请求的资源不支持 'GET'

[英]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.你好,我有一个注册方法,我正在尝试使用它实际上是一个 POST 但返回一个 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尝试了这个解决方案https://docs.microsoft.com/en-us/aspnet/web-api/overview/testing-and-debugging/troubleshooting-http-405-errors-after-publishing-web-api-applications没有成功

You have to update your Register method to allow Post calls.您必须更新您的 Register 方法以允许 Post 调用。

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.我之前遇到过这个问题(405 : Method not allowed),删除 WebDav 发布功能解决了这个问题。 You can go to Add/Remove features and under IIS, try removing Web Dav publishing.您可以转到添加/删除功能,然后在 IIS 下尝试删除 Web Dav 发布。 Below screen shot taken from windows 10 console (it might vary depending upon server OS)下面是从 Windows 10 控制台截取的屏幕截图(它可能因服务器操作系统而异)

And yes, I believe [HttpPost] annotation is required.是的,我相信 [HttpPost] 注释是必需的。

在此处输入图片说明

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.在您的控制器中,您将有两个注册方法,一个带有 get 注释以返回视图,第二个带有 post 注释以创建或注册用户本身。 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);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Web API - 405 - 请求的资源不支持http方法&#39;PUT&#39; - Web API - 405 - The requested resource does not support http method 'PUT' WebApi Post 方法总是返回“请求的资源不支持 http 方法 &#39;GET&#39;。” 状态:405 方法不允许 - WebApi Post Methods always Returns “The requested resource does not support http method 'GET'.” Status: 405 Method Not Allowed 请求的资源不支持HTTP方法GET - The requested resource does not support HTTP method GET 请求的资源不支持 HTTP 方法“GET” - The requested resource does not support HTTP method 'GET' WebApi 2 Http Post 405“请求的资源不支持http方法&#39;POST&#39;” - WebApi 2 Http Post 405 “The requested resource does not support http method 'POST'” c#webapi-请求的资源不支持http方法&#39;GET&#39; - c# webapi - The requested resource does not support http method 'GET' 所请求的资源在Visual Studio 2010中不支持HTTP方法“ GET” - The requested resource does not support HTTP method 'GET' in visual studio 2010 请求的资源不支持 http 方法“GET”,但使用“POST” - The requested resource does not support http method 'GET' but using 'POST' ASP.Net-请求的资源不支持http方法“ GET” - ASP.Net - Requested resource does not support http method 'GET' WebApi-请求的资源不支持http方法“ GET” - WebApi - The requested resource does not support http method 'GET'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM