【问题标题】:How to fix 405 method not allowed?如何修复不允许的 405 方法?
【发布时间】:2020-01-10 06:35:44
【问题描述】:

我一直在尝试找到有关 405 方法不允许问题的解决方案。有一个用于登录和注册的api。注册 post 方法工作正常,但是当我从邮递员发送凭据时,我收到 405 错误。我已经搜索了解决方案,<remove name="WebDAVModule" /> 的答案非常广泛,但它并没有解决我的问题。我该如何解决?

CreateUser 工作正常。

   [HttpPost]
        [Route("CreateUser")]
        public async Task<IActionResult> CreateUser([FromBody]RegisterVm register)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var userIdentity = _mapper.Map<User>(register);
            var result = await _userBusiness.CreateUser(userIdentity, register.Password).ConfigureAwait(true);
            if (!result.Succeeded) return new BadRequestObjectResult(Errors.AddErrorsToModelState(result, ModelState));

            return new OkObjectResult("Account created");
        }

无法登录。

[HttpPost]
    [Route("Login")]
    public async Task<IActionResult> Login([FromBody]LoginVm login)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        var identity = await GetClaimsIdentity(login.Email, login.Password);
        if (identity == null)
        {
            return BadRequest(Errors.AddErrorToModelState("login_failure", "Invalid username or password.", ModelState));
        }

        var jwt = await Tokens.GenerateJwt(identity, _jwtFactory, login.Email, _jwtOptions, new JsonSerializerSettings { Formatting = Formatting.Indented }).ConfigureAwait(true);
        return new OkObjectResult(jwt);
    }

【问题讨论】:

  • 你可以参考我的回答here

标签: api asp.net-core .net-core


【解决方案1】:

老实说,只有两个原因:

  1. 用户代理意外发送了错误的 HTTP 方法。
  2. 服务器只希望为所请求的资源提供少数有效的 HTTP 方法,这些方法不包括您需要的内容。

由于 2 显然不是答案,您需要确保您的调用者实际上是 POSTING 数据,而不是使用 GET、PUT、DELETE 或任何其他动词。

用于调试和解决此问题的 Microsoft 文档:https://docs.microsoft.com/en-us/aspnet/web-api/overview/testing-and-debugging/troubleshooting-http-405-errors-after-publishing-web-api-applications

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 2021-12-20
    • 2023-03-23
    相关资源
    最近更新 更多