How to pass multiple parameters to a get method in ASP.NET Core

前端 未结 12 1415
说谎
说谎 2020-12-04 07:46

How can I pass in multiple parameters to Get methods in an MVC 6 controller. For example I want to be able to have something like the following.

[Route(\"api         


        
12条回答
  •  眼角桃花
    2020-12-04 08:06

    I would suggest to use a separate dto object as an argument:

    [Route("api/[controller]")]
    public class PersonController : Controller
    {
        public string Get([FromQuery] GetPersonQueryObject request)
        {
            // Your code goes here
        }
    }
    
    public class GetPersonQueryObject 
    {
        public int? Id { get; set; }
        public string Firstname { get; set; }
        public string Lastname { get; set; }
        public string Address { get; set; }
    }
    

    Dotnet will map the fields to your object.

    This will make it a lot easier to pass through your parameters and will result in much clearer code.

提交回复
热议问题