小编Eri*_*han的帖子

在ASP.NET Core Web API Controller中使用C#7元组

你知道为什么会这样吗:

    public struct UserNameAndPassword
    {
        public string username;
        public string password;
    }


    [HttpPost]
    public IActionResult Create([FromBody]UserNameAndPassword usernameAndPassword)
    {
        Console.WriteLine(usernameAndPassword);
        if (this.AuthenticationService.IsValidUserAndPasswordCombination(usernameAndPassword.username, usernameAndPassword.password))
            return new ObjectResult(GenerateToken(usernameAndPassword.username));
        return BadRequest();
    }
Run Code Online (Sandbox Code Playgroud)

但是当我用元组替换它时,这不起作用?

     [HttpPost]
    public IActionResult Create([FromBody](string username, string password) usernameAndPassword) //encrypt password?
    {
        Console.WriteLine(usernameAndPassword);
        if (this.AuthenticationService.IsValidUserAndPasswordCombination(usernameAndPassword.username, usernameAndPassword.password))
            return new ObjectResult(GenerateToken(usernameAndPassword.username));
        return BadRequest();
    }
Run Code Online (Sandbox Code Playgroud)

usernameAndPassword.username和.password都为null.

你不被允许在控制器中使用元组吗?

c# tuples asp.net-core c#-7.0 asp.net-core-webapi

13
推荐指数
2
解决办法
3206
查看次数

标签 统计

asp.net-core ×1

asp.net-core-webapi ×1

c# ×1

c#-7.0 ×1

tuples ×1