.net core 应用程序出现不支持的媒体类型错误

问题描述 投票:0回答:1

我有 jwt web api .net 核心应用程序。 Razor 页面出现以下错误。应用程序在 Swagger 和 postman 中完美运行。

{“type”:“https://tools.ietf.org/html/rfc7231#section-6.5.13”,“title”:“不支持的媒体类型”,“status”:415,“traceId”:“00 -600b42fb68fbfa47dc71ff763d7bfa71-03a493705b62904e-00"}

    <form method="post" asp-route="Form" action="api/Auth/LoginUser" id="form">
            <div class="form-group f">
                <label for="InputUsername">Username</label>
                <input type="text" class="form-control" name="username" placeholder="Username">
            </div>
            <div class="form-group f">
                <label for="InputPassword">Password</label>
                <input type="password" class="form-control" name="password" placeholder="Password">
            </div>
            <button type="submit" class="btn btn-primary">Submit</button>
        </form>
    public void OnPost([FromBody] UserLoginRequest request)
            {
                Request.ContentType = "application/json";
    
                request.username = Request.Form["username"];
                request.password = Request.Form["password"];
            }
[HttpPost("LoginUser")]
        [AllowAnonymous]
        public async Task<ActionResult<UserLoginResponse>> LoginUserAsync([FromBody] UserLoginRequest request)
        {
            var result = await authService.LoginUserAsync(request);

            return result;
        }
asp.net-mvc asp.net-core
1个回答
0
投票

对于表单,您应该使用 [FromForm] 属性而不是 [FromBody] 属性。

© www.soinside.com 2019 - 2024. All rights reserved.