1

我来自一个沉重的 Java/Spring 背景,并试图将一些知识转移到 ASP.NET Core 6。

在 Spring 中RestController,我可以根据查询参数的存在来路由请求。

因此,HttpRequest带有 uri:/students?firstName=Kevin的 a 可以路由到与带有 uri: 的 a 不同的控制器HttpRequest方法/students

在 ASP.NET Core 6 中,在完成一些示例并阅读 Web API 文档后,我无法确定是否可以实现等价物。

这是我要实现的目标,这是否可以使用两种方法和路由配置来识别根据查询参数调用哪个控制器方法?

 [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 */
    }
 }
4

3 回答 3

2

您正在尝试使用区分 API 调用query params。这不是这样做的方法。如果你想分开你可能应该使用的电话path params

阅读更多关于ASP.NET Core 中的路由- https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-6.0

于 2022-01-08T12:28:43.447 回答
1

我想你正在寻找这样的东西,你需要在“ HttpGet ”属性中指定参数

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);
    }
}
于 2022-01-08T12:25:31.957 回答
1

虽然 ASP.NET Core 并没有开箱即用地提供按查询参数进行过滤,但您自己提供此功能并不难。

在可扩展性方面,ASP.NET 有一些超能力,其中之一就是IActionConstraint.

支持条件逻辑来确定关联的操作是否可以为给定的请求选择有效。(来源

创建注释以过滤查询参数就像

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class QueryParameterConstraintAttribute : Attribute, IActionConstraint
{
    private readonly string _parameterName;

    public QueryParameterConstraintAttribute(string parameterName)
    {
        this._parameterName = parameterName;
    }

    public bool Accept(ActionConstraintContext context)
    {
        return context.RouteContext.HttpContext.Request.Query.Keys.Contains(this._parameterName);
    }

    public int Order { get; }
}

剩下的就是用那个约束注释你的控制器方法

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

在快速测试中,我能够确认它似乎按预期工作,即使您为不同的查询参数添加了多个这些属性(如果所有条件都匹配,则调用路由)。

(请注意,这是使用 .NET Core 2.1 测试的。无论如何,它应该与 .NET 6 几乎相同)

于 2022-02-22T06:21:08.060 回答