DZone

My favourite feature with .Net and C# is that it’s a static programming language. This is also the largest problem I have with .Net 5 – As in, its blessing also becomes its curse. Simply because sometime I need a bit more “dynamic nature” than that which .Net provides out of the box. However, .Net 5 has a lot of interesting features, allowing you to circumvent this, opening it up for a more “dynamic nature”. Let me illustrate with an example C# HTTP API controller.

C#

 

<div class="codeMirror-code–wrapper" data-code="[Route("magic")]
public class EndpointController : ControllerBase
{
[HttpGet]
[Route("{*url}")]
public async Task Get(string url)
{
/* Do interesting stuff with URL here */
}
}” data-lang=”text/x-csharp”>

x
9

10

 

1

[Route("magic")]

2

public class EndpointController : ControllerBase

3

{

4

    [HttpGet]

5

    [Route("{*url}")]

6

    public async Task<ActionResult> Get(string url)

7

    {

8

        /* Do interesting stuff with URL here */

9

    }

10

}

Notice the [Route(“{*url}”)] parts in the above code? This tiny little detail implies that everything going towards “magic/xxx” will be resolved by the controller, and its “xxx” parts will end up becoming the url argument to our method.

Source: DZone