如何在 API 端点上添加后缀 .NET Core

发布于 2025-01-11 01:07:20 字数 356 浏览 0 评论 0原文

有没有办法在所有端点路由上自动添加后缀,例如 .json。

v1/users.json
v1/users/{id}.json

所以到目前为止我尝试过的是我创建了一个 BaseController ,它看起来像这样

[ApiController]
[Route("v1/[controller].json")]
public class BaseController : ControllerBase
{
}

,但每次我将它用于我的控制器时,它看起来像这样

v1/users.json
v1/users.json/{id}

Is there a way to automatically add a suffix on all endpoint routes e.g .json.

v1/users.json
v1/users/{id}.json

so what I have tried so far is I created a BaseController which look like this

[ApiController]
[Route("v1/[controller].json")]
public class BaseController : ControllerBase
{
}

but every time I use it to my controller it looks like this

v1/users.json
v1/users.json/{id}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何必那么矫情 2025-01-18 01:07:20

您可以将额外的路由添加到实际端点而不是控制器

[ApiController]
[Route("v1/[controller]")]
public class BaseController : ControllerBase
{
    [HttpGet(".json")]
    public IActionResult Get()
    {
    
    }
    
    // Without Route Parameters
    [HttpGet("{id}.json")]
    public IActionResult Get([FromRoute] int id)
    {
          ...
    }

    // With Route and Query Parameters
    [HttpGet("{id}.json/friend")]
    public IActionResult Get([FromRoute]int id,[FromQuery] string friendName)
    {
          ...
    }

    // With Route and Query Parameters and Body
    [HttpPost("{id}.json/friends")]
    public IActionResult Get([FromRoute]int id,[FromQuery] string message, [FromBody]IFilter filter)
    {
          ...
    }
}

You can add extra routing to the actual endpoints rather than the controllers

[ApiController]
[Route("v1/[controller]")]
public class BaseController : ControllerBase
{
    [HttpGet(".json")]
    public IActionResult Get()
    {
    
    }
    
    // Without Route Parameters
    [HttpGet("{id}.json")]
    public IActionResult Get([FromRoute] int id)
    {
          ...
    }

    // With Route and Query Parameters
    [HttpGet("{id}.json/friend")]
    public IActionResult Get([FromRoute]int id,[FromQuery] string friendName)
    {
          ...
    }

    // With Route and Query Parameters and Body
    [HttpPost("{id}.json/friends")]
    public IActionResult Get([FromRoute]int id,[FromQuery] string message, [FromBody]IFilter filter)
    {
          ...
    }
}
假情假意假温柔 2025-01-18 01:07:20

您可以使用 URL 重写中间件来接受带有 .json 的 URL,然后将其删除。因此,类似:

/api/users/123/picture.json?query=123

将变为:

/api/users/123/picture?query=123

您可以通过将以下代码添加到启动的 Configure 方法中来完成此操作:

var rewriteOptions = new RewriteOptions()
    .AddRewrite(@"^(.*?)(?:\.json)(\?.*)?$", "$1$2");
app.UseRewriter(rewriteOptions);

请参阅文档 了解更多信息。

注意:如果您使用 Url.Action(...) 等在代码中生成 URL,则它不会包含 .json。重写仅影响传入请求。

You could use the URL Rewriting Middleware to accept URLs with .json and then simply remove it. So something like:

/api/users/123/picture.json?query=123

would become:

/api/users/123/picture?query=123

You can do this by adding the following code to your Startup's Configure method:

var rewriteOptions = new RewriteOptions()
    .AddRewrite(@"^(.*?)(?:\.json)(\?.*)?
quot;, "$1$2");
app.UseRewriter(rewriteOptions);

See the docs for more information.

Caveat: If you use Url.Action(...), etc. to generate a URL within code, then it won't include .json. The rewrite only affects incoming requests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文