简体   繁体   English

Web Api .NET Core的简单POST

[英]Simple POST with Web Api .NET Core

I can't produce my code despite the different questions and answers found on stackoverflow. 尽管在stackoverflow上发现了不同的问题和答案,但我无法生成代码。

Here is my problem, I want to receive the Username and Password of the client to perform the verification (the equality is only for the example). 这是我的问题,我想接收客户端的UsernamePassword来执行验证(相等性仅用于示例)。

However the LoginPost variable is all the time null . 但是, LoginPost变量始终为null Furthermore I have difficulty understanding the best way to send the client an http code and json at the same time. 此外,我很难理解同时发送客户端http代码和json的最佳方法。

using Microsoft.AspNetCore.Mvc;
using web.Models;

namespace web.Controllers
{
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class LoginController : ControllerBase
    {
        [HttpPost]
        public ActionResult<LoginPost> LoginPost([FromBody] LoginPost loginPost)
        {
            // (1) FAIL, loginPost variable is null

            if (loginPost.Username == loginPost.Password)
            {
                return loginPost;
            }


            // (2) How to add a message like "User/Password fails"

            return Unauthorized(); 
        }

    }
}

Note the annotations (1) and (2) in the code. 请注意代码中的注释(1)和(2)。

Here's the jQuery code 这是jQuery code

$.ajax({
        url: '../api/Login',
        type: 'POST',
        dataType: "json",
        contentType: "application/json, charset=utf-8",
        data: {
            Username: username,
            Password: password
        },
        statusCode: {
            200: function (data) {
                console.log(data);
            }
            401: function (data) {
                console.log(data);
            }
            500: function (data) {
                console.log(data);
            }
        }
});

and LoginPost.cs class: LoginPost.cs类:

public class LoginPost
{
    public string Username { get; set; }
    public string Password { get; set; }
}

Since you don't seem to want to use json , post the data as a form. 由于您似乎不想使用json ,因此请以表格形式发布数据。

Remove contentType: "application/json, charset=utf-8", from your ajax call. 从您的Ajax调用中删除contentType: "application/json, charset=utf-8",

'application/x-www-form-urlencoded; charset=UTF-8' 'application/x-www-form-urlencoded; charset=UTF-8' is the default so the data will be sent as a form. 'application/x-www-form-urlencoded; charset=UTF-8'是默认值,因此数据将以表格形式发送。 Also remove [FromBody] Attribute and it should work 同时删除[FromBody]属性,它应该可以工作

对于(2)问题,您可以返回状态代码:

return StatusCode(401, "User/Password fails");

The login controller is working fine, I tested it with Postman. 登录控制器工作正常,我在Postman上进行了测试。 The problem lies with the ajax call: it does not send the data as the body of the POST request but as URL query parameters (ie Username=username&Password=password ). 问题在于ajax调用:它不将数据作为POST请求的主体发送,而是作为URL查询参数(即Username=username&Password=password )发送。

To send the data in the body of the POST request you need to send the data as json string: 要在POST请求的正文中发送数据,您需要将数据作为json字符串发送:

data: JSON.stringify({
    "Username": "username",
    "Password": "password"
}),

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM