捕获所有以GUID结尾的URL

时间:2019-06-04 14:25:44

标签: model-view-controller asp.net-core-mvc asp.net-mvc-routing

我正在.Net Core 2.1中构建一个API,并具有一个分页系统,用户可以在其中请求大容量的卷,并以可配置的大小块返回。发送块时,它将包含一个GUID,该GUID是对下一个数据块的引用

例如,如果我们有一个匹配项返回526个结果,它将把数据分块为5 x 100和1 x 26记录块,第一个匹配项来自 GET /事件/

第二页数据将来自类似 GET / events / 95d9f018-bff9-46e7-ad86-9b9d6734cc0d

每种控制器方法上已经有很多路由

[HttpGet("/events/"]
public ActionResult GetAllEvents() {}

[HttpGet("/events/{EventId}"]
public ActionResult GetEvent(int EventId) {}

[HttpGet("/events/after/{AfterDate}"]
public ActionResult GetEventsAfter(DateTime AfterDate) {}

,在某些情况下,方法可以具有多个路由值。我要添加的内容是全部捕获,如果URL以GUID结尾,则路由会有所不同。

此刻我看到的是,如果我添加一个GUID,它将进入“ GetEvent”方法

我尝试在MVC中添加路由

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "Continuation",
        template: "Route/{*ContinuationToken}",
        defaults: new { controller = "Continuation", action = "GetPagedResponse" },
        constraints: new { ContinuationToken = @"(?im)^[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$" }
    );
});     

但是很明显,这不是正确的实现方法,因为请求仍然路由到原始控制器和操作,而不是ContinuationController

是否可以将以Guid结尾的所有请求路由到特定 控制器和动作?

1 个答案:

答案 0 :(得分:0)

尝试这样做,

在要传递延续令牌的Controller中,添加此代码段,

        [Route("/continuation/{continuationToken:guid}")]
        public IActionResult nextResult(Guid continuationToken)
        {
            return View();
        }

在您的Startup.cs中,我有这个内容,类似于您的示例。

routes.MapRoute(
                    name: "guidroute",
                    template: "{controller}/{*guid:guid}",
                    defaults : new { controller = "continuation"}
                     );

,并将其添加到默认路由之前。