Core API Controller to catch all unknown routes

蹲街弑〆低调 提交于 2020-08-26 05:47:43

问题


I have a Core 2.2 API with a bunch of existing controllers. What i am now trying to do is add a new controller that acts similar to a catchall route, but only for that controller (and doesn't disturb the routes of the existing controllers).

In my existing controller i am defining the routes as controller attributes

[Route("api/[controller]")]
[ApiController]
public class SandboxController : ControllerBase
{
    [HttpGet("Hello")]
    public IEnumerable<string> Hello()
    {
        return new string[] { "Hello World", TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time")).ToString()};
    }
}

For this new "catchall" controller i need it to be able to catch any Get, Post, Put, Delete that is routed to it. For example this controllers route is ../api/catchall. If someone where to do a post to ../api/catchall/some/random/unknown/route i'm trying to catch this and route it to ../api/catchall/post.

So far i have be utterly unsuccessful. This is what i got so far:

In my Startup.cs

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseAuthentication();

...

    app.UseMvc(routes =>
    {
        routes.MapRoute("default", "{controller=Sandbox}/{action=Hello}/{id?}");

        routes.MapRoute(
            name: "catchall",
            template: "{controller}/{*.}", 
            defaults: new { controller = "catchall", action = "post" });
    });

And the catchall controller:

[Route("api/[controller]")]
[ApiController]
public class CatchallController : ControllerBase
{
    [HttpPost("post", Order = int.MaxValue)]
    public IActionResult Post([FromBody] string value)
    {
        return Content("{ \"name\":\"John Doe\", \"age\":31, \"city\":\"New York\" }", "application/json");
    }
}

Any ideas on how i can get this to work?


回答1:


Catch all routes are specified with the * or ** syntax. Place [Route("{**catchall}")] on the action that you want to be the catch all action. This will make a catch all route for all routes prefixed with the prefix specified in the Controller route attribute.

[Route("api/[controller]")]
[ApiController]
public class CatchallController : ControllerBase
{
    [Route("{**catchAll}")]
    [HttpPost("post", Order = int.MaxValue)]
    public IActionResult Post([FromBody] string value, string catchAll)
    {
        return Content("{ \"name\":\"John Doe\", \"age\":31, \"city\":\"New York\" }", "application/json");
    }
}

In the example above, this will catch api/catchall/anything/following/it and set the string catchAll to anything/following/it

If you want to set a site-wide catch all route you can use an absolute url

[Route("/{**catchAll}")]
public IActionResult CatchAll(string catchAll)
{

}

This will catch any route that does not match any other specified route.



来源:https://stackoverflow.com/questions/56587342/core-api-controller-to-catch-all-unknown-routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!