核心API控制器可捕获所有未知路由 [英] Core API Controller to catch all unknown routes

查看:114
本文介绍了核心API控制器可捕获所有未知路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有大量现有控制器的Core 2.2 API.我现在想做的是添加一个新控制器,该控制器的行为类似于全能路由,但仅适用于该控制器(并且不会干扰现有控制器的路由).

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()};
    }
}

对于这个新的"catchall"控制器,我需要它能够捕获路由到它的任何Get,Post,Put,Delete .例如,此控制器的路由为../ api/catchall .如果有人要在../api/catchall/一些/随机/未知/路线中发布信息,我正在尝试捕捉并发送至../api/catchall/发布.

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:

在我的Startup.cs中

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" });
    });

和综合控制器:

[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?

推荐答案

放在要成为全部捕获动作的动作上.这样将捕获所有以Controller路由属性中指定的前缀为前缀的所有路由的所有路由.

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");
    }
}

在上面的示例中,这将捕获api/catchall/anything/following/it并将字符串catchAll设置为anything/following/it

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.

这篇关于核心API控制器可捕获所有未知路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆