【问题标题】:Matrix URI and ASP.NET Core矩阵 URI 和 ASP.NET Core
【发布时间】:2019-01-04 20:37:25
【问题描述】:

想知道是否有人尝试将Matrix URI 与 ASP.NET Core 一起使用?

我们正在构建一个 Angular 前端,它使用他们称为 Route Parameters 的版本。前端将需要对我们的后端进行类似的调用,并且它基于 ASP.NET Core 构建。但是,我真的无法在 Microsoft 的路由文档中找到有关它的任何信息。也许微软用不同的名字来称呼它,就像 Angular 一样。

矩阵 URI 类似于:http://some.url.com;foo=bar;baz=bing

有几个很好的答案可以谈论它们是什么,我似乎找不到任何与 ASP.NET Core 相关的东西。

任何关于在 ASP.NET Core API 中实现矩阵 URI 的信息/建议将不胜感激。谢谢。

【问题讨论】:

  • 您的问题已经开放了 9 个月,没有答案!你有没有让 Matrix URI 在 Asp.Net Core 中工作?我也一直在研究它,但在 Asp.Net Core 文档中没有提到它
  • 不,很遗憾,我在上面找不到任何东西。我们走了一个不同的方向,所以这不再是问题了。

标签: angular asp.net-core route-parameters


【解决方案1】:

我最近一直在考虑类似的事情。矩阵 URI 并不是什么新鲜事物,但并未广为人知或使用。我偶然发现了这个问题,空手而归。

经过更多研究,我决定编写一个自定义模型绑定器。我首先创建一个存根来探索自定义模型绑定器是如何工作的,但是当我将它连接到我的控制器方法时,我有一个随机的想法……如果我只是使用 HttpGet 模板告诉 Web API 如何解析 URI并提取矩阵属性。

成功了!代码如下:

/// <summary>
/// Retrieve a file resource by guid identifier
/// </summary>
/// <param name="id">A guid representing the generated ID of the resource</param>
/// <param name="fragment">hash of fragment being requested</param>
/// <returns></returns>
/// <response code="200">Returns the requested resource</response>
/// <response code="400">One or more of the values given was invalid</response>
/// <response code="404">The requested resource was not found</response>
/// <response code="303">Redirect to canonical/stable identity</response>
[HttpGet("file/id/{id};fragment={fragment}")]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status303SeeOther)]
public async Task<IActionResult> GetById(Guid? id, string fragment)
{
    if (id == null)
        return BadRequest("");

    ...

    return File(bytes, contentType, fileName);
}

FWIW 我正在使用 .net 5,但我有理由确定这至少在 .net core 3 中有效。

希望这可以帮助下一个解决这个问题的人!

【讨论】:

  • 好主意,我有兴趣尝试一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-22
  • 2012-12-03
相关资源
最近更新 更多