多个不同位置上带参数的方法的路由

huangapple go评论72阅读模式
英文:

Multiple routes on method with parameter in different locations

问题

I'm translating an old API to .net 7, while at the same time creating some new routes for some methods that make more sense than the old ones, while maintaining the old ones for compatibility.

我正在将一个旧的 API 转换为 .NET 7,同时创建一些新的路由来替代一些比旧的更合理的方法,同时保留旧方法以保持兼容性。

I have some cases where a parameter is being passed as a query parameter but it would make more sense to pass it as a route parameter. The only way I have found of making this work is by creating two similar methods, one where the parameter is declare with FromQuery attribute and another where the parameter is declared with FromRoute attribute. Like this:

有些情况下,参数作为查询参数传递,但将其作为路由参数传递会更合理。我发现使这项工作的唯一方法是创建两个类似的方法,一个在该方法中使用 FromQuery 属性声明参数,另一个在该方法中使用 FromRoute 属性声明参数。如下:

[Route("value")]
public IActionResult GetValue1([FromQuery(Name = "param")] int parameter)
{
    ...
}

[Route("value/{param}")]
public IActionResult GetValue2([FromRoute(Name = "param")] int parameter)
{
    ...
}

Is it possible to declare just one instance of this method that would accept the parameter by route or query, or do I have to keep using two separate methods for this?

是否可能只声明一个此方法的实例,以接受路由或查询参数,或者我必须继续使用两个单独的方法来处理这个?

[Edit]

I have created a small project to test this with one method declared like this:

我创建了一个小项目来测试这个,其中有一个方法的声明如下:

[HttpGet]
[Route("string")]
[Route("string/{str}")]
public string GetString(string str)
{
    return $"abc + {str}";
}

If I call the method with the route parameter, like this:

如果我使用路由参数调用该方法,如下所示:

/string/123

then I get the expected reply abc + 123.

那么我会得到预期的回复 abc + 123

But if I call the method with a query parameter, like this:

但如果我使用查询参数调用该方法,如下所示:

/string?str=123

then I get an error: The str field is required.

但如果我使用查询参数调用该方法,我会收到错误消息:str 字段是必需的

If I change the method parameter to be nullable (string?) then it does not return an error, but it does not pick up the parameter value from the query, it simply returns abc + .

如果我将方法参数更改为可为 null(string?),那么它不会返回错误,但它不会从查询中获取参数值,它只会返回 abc +

英文:

I'm translating an old API to .net 7, while at the same time creating some new routes for some methods that make more sense than the old ones, while maintaining the old ones for compatibility.

I have some cases where a parameter is being passed as a query parameter but it would make more sense to pass it as a route parameter. The only way I have found of making this work is by creating two similar methods, one where the parameter is declare with FromQuery attribute and another where the parameter is declared with FromRoute attribute. Like this:

[Route("value")]
public IActionResult GetValue1([FromQuery(Name = "param")] int parameter)
{
    ...
}

[Route("value/{param}")]
public IActionResult GetValue2([FromRoute(Name = "param")] int parameter)
{
    ...
}

Is it possible to declare just one instance of this method that would accept the parameter by route or query, or do I have to keep using two separate methods for this?

[Edit]

I have created a small project to test this with one method declared like this:

[HttpGet]
[Route("string")]
[Route("string/{str}")]
public string GetString(string str)
{
    return $"abc + {str}";
}

If I call the method with the route parameter, like this:

/string/123

then I get the expected reply abc + 123.

But if I call the method with a query parameter, like this:

/string?str=123

then I get an error: The str field is required.

If I change the method parameter to be nullable (string?) then it does not return an error, but it does not pick up the parameter value from query, it simply returns abc + .

答案1

得分: 1

以下代码有效。

[HttpGet]
[Route("string/{str1?}")]
public string GetString([FromRoute]string? str1, [FromQuery]string? str)
{
    if (str1 != null) { str = str1; }
    return $"abc + {str}";
}
英文:

The following code works.

[HttpGet]
[Route("string/{str1?}")]
public string GetString([FromRoute]string? str1, [FromQuery]string? str)
{
    if (str1 != null) { str = str1; }
    return $"abc + {str}";
}

huangapple
  • 本文由 发表于 2023年4月6日 19:41:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定