Matrix URI and ASP.NET Core

414 Views Asked by At

Wondering if anyone has attempted to use Matrix URI's with ASP.NET Core?

We're building an Angular front-end that uses their version called Route Parameters. The front-end will need to make a similar call to our back-end and it's build on ASP.NET Core. However I'm not really able to locate any information about it in Microsoft's routing docs. Perhaps Microsoft is calling it by a different name, like Angular does.

Matrix URI looks like: http://some.url.com;foo=bar;baz=bing

There are a couple of good answers that talk about what they are, I just can't seem to find anything related to ASP.NET Core.

Any information/advice on implementing matrix URI in an ASP.NET Core API would be greatly appreciated. Thanks.

1

There are 1 best solutions below

1
On

I've been thinking about something similar recently. Matrix URIs are nothing new, but not widely known or used. I stumbled on this question and left empty handed.

After more research, I settled on writing a custom modelbinder. I started by creating a stub to explore how custom modelbinders work but when I went to wire it up to my controller method, I had a random thought... What if I just used the HttpGet template to tell Web API how to parse the URI and extract the matrix properties.

It worked! Here's the code:

/// <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 I'm using .net 5 but I'm reasonably certain this works at least as of .net core 3.

Hopefully this helps the next person who lands on this question!