Asp .Net Core 3.1 Web Api catch all controller subroutes http requests

1.7k Views Asked by At

I would like to catch the path of my controller as a string in order to process every requests:

     [ApiController]
        [Route("[controller]")]
        public class BackendController : ControllerBase
        {
            [Route("Backend/{url}")]
            public Task<HttpResponseMessage> CatchAll(string url)
            {
     var requestmessage = HttpContext.Request;
//Do stuff
            }
    }

I found a lot of thing in asp.net core 2.1 but I didn't managed to make it work with my solution..

Thanks in advance

3

There are 3 best solutions below

2
On
    [ApiController]
    [Route("api/[controller]/[action]/{url}")]
    public class BackendController : ControllerBase
    {
        [HttpGet]
        public IActionResult GenericAction(string url)
        {
            var queryString = HttpContext.Request.QueryString;
            return Ok($"url: {url}, queryString: {queryString}");
        }
    }

The result from above code:

Url: https://127.0.0.1:5001/api/backend/GenericAction/product?id=100

Response: url: product, queryString: ?id=100

url: https://127.0.0.1:5001/api/backend/GenericAction/company?name=michael

Response: url: company, queryString: ?name=michael

Edit

If you want to add all http action to one

        [HttpGet]
        [HttpPost]
        [HttpPut]
        [HttpDelete]
        public IActionResult GenericAction(string url)
        {
            var queryString = HttpContext.Request.QueryString;
            return Ok($"url: {url}, queryString: {queryString}");
        }

Edit

Without action name

    [ApiController]
    public class BackendController : ControllerBase
    {
        [HttpGet]
        [HttpPost]
        [HttpPut]
        [HttpDelete]
        [Route("api/backend/{url}")]
        public IActionResult Get(string url)
        {
            var queryString = HttpContext.Request.QueryString;
            return Ok($"url: {url}, queryString: {queryString}");
        }
    }
0
On

If you want to get the path of request,and process every requests,you can use middleware.

Here is a demo:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.Use(async (context, next) =>
            {
                //get path
                var path = context.Request.Path.ToString();
                //when path == what you want,do something
                if (path == "xxxx")
                {
                    //do something
                }
                await next();
            });
            ...................
        }
1
On

You can use round-tripping route parameter syntax :

Route   Sample
/search/{*page}     /search/admin%2Fproducts (the forward slash is encoded)
/search/{**page}    /search/admin/products

I'm using CatchAll template to catch all routes and methods for my Echo sample :

[ApiController]
[Route("{**catchAll}")]
public class EchoController : ControllerBase
{
    [Route("/{**catchAll}")]
    public IActionResult Echo()
    {
        return Ok();
    }
}

More info on Microsoft Docs