简体   繁体   English

ASP.NET Core Web API:通过查询参数路由

[英]ASP.NET Core Web API : route by query parameter

I am coming from a heavy Java/Spring background and trying to transition some knowledge over to ASP.NET Core 6.我来自沉重的 Java/Spring 背景,并试图将一些知识转移到 ASP.NET Core 6。

In Spring, on a RestController , I am able to route the request based on the presence of a query parameter.在 Spring 中,在RestController上,我能够根据查询参数的存在来路由请求。

So a HttpRequest with the uri: /students?firstName=Kevin can be routed to a different controller method than a HttpRequest with the uri: /students .因此,与带有 uri: /students 的HttpRequest相比,带有 uri: /students /students?firstName=KevinHttpRequest可以路由到不同的 controller 方法。

In ASP.NET Core 6, I am unable to determine if the equivalent is possible after working through some examples and reading the documentation for Web API.在 ASP.NET Core 6 中,在完成一些示例并阅读 Web API 的文档后,我无法确定是否可以进行等效操作。

Here is what I am trying to achieve, is this possible using two methods and routing configuration that will discern which controller method to invoke based on the query parameter?这是我要实现的目标,这是否可以使用两种方法和路由配置来识别根据查询参数调用哪个 controller 方法?

 [ApiController]
 [Route("Students")]
 public class StudentHomeProfileController : ControllerBase
 {
    [HttpGet] //Route here when no parameters provided
    public async Task<ActionResult<IEnumerable<Student>>> GetStudentAsync()
    {
        /* Code omitted */
    }

    [HttpGet] //Route here when firstName query param provided
    public async Task<ActionResult<IEnumerable<Student>>> SearchStudentAsync([FromQuery] string firstName)
    {
        /* Code omitted */
    }
 }

You are trying to differentiate API calls using query params .您正在尝试使用query params来区分 API 调用。 this is not the way to do this.这不是这样做的方法。 if you want to separate the calls you should probably use path params instead.如果你想分开调用,你可能应该使用path params

Read more about Routing in ASP.NET Core - https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-6.0阅读有关ASP.NET 核心中的路由的更多信息 - https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-6.0

I think you are looking for something like this, you need to specify the parameter in the " HttpGet " attribute我想你正在寻找这样的东西,你需要在“ HttpGet ”属性中指定参数

https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-6.0#attribute-routing-with-http-verb-attributes https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-6.0#attribute-routing-with-http-verb-attributes

[Route("api/[controller]")]
[ApiController]
public class Test2Controller : ControllerBase
{
    [HttpGet]   // GET /api/test2
    public IActionResult ListProducts()
    {
        return ControllerContext.MyDisplayRouteInfo();
    }

    [HttpGet("{id}")]   // GET /api/test2/xyz
    public IActionResult GetProduct(string id)
    {
       return ControllerContext.MyDisplayRouteInfo(id);
    }

    [HttpGet("int/{id:int}")] // GET /api/test2/int/3
    public IActionResult GetIntProduct(int id)
    {
        return ControllerContext.MyDisplayRouteInfo(id);
    }

    [HttpGet("int2/{id}")]  // GET /api/test2/int2/3
    public IActionResult GetInt2Product(int id)
    {
        return ControllerContext.MyDisplayRouteInfo(id);
    }
}

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

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