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

查看:18
本文介绍了核心 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/some/random/unknown/route 我正试图抓住这个并将其路由到 ../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?

推荐答案

捕获所有路由 使用 *** 语法指定.将 [Route("{**catchall}")] 放在您希望成为捕获所有操作的操作上.这将为所有以 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

如果你想设置一个站点范围的捕获所有路由,你可以使用绝对 url

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天全站免登陆