简体   繁体   English

ASP.Net Core 2.2 升级 - 默认路由重定向问题

[英]ASP.Net Core 2.2 Upgrade - Default Route Redirection Issue

Until Asp.Net Core 2.1, I was using below code in order to redirect default route to swagger endpoint.在 Asp.Net Core 2.1 之前,我使用以下代码将默认路由重定向到 swagger 端点。

app.UseMvc(builder =>
{
    builder.MapRoute("default", template: "api/{controller}/{action}/{id?}");
    builder.MapGet("", context =>
    {
        context.Response.Redirect("./swagger/index.html", permanent: false);
        return Task.FromResult(0);
    });

});

However, when I upgraded to Asp.Net Core 2.2 this code did not work as expected (redirection is called)但是,当我升级到 Asp.Net Core 2.2 时,这段代码没有按预期工作(调用重定向)

Is this known issue?这是已知问题吗? How can I solve it?我该如何解决?

Edit: Thanks to @KirkLarkin编辑:感谢@KirkLarkin

I have changed options.EnableEndpointRouting to false and now it is working.我已将options.EnableEndpointRouting更改为false ,现在它正在工作。 However as I unsderstand this is legacy routing method.但是,据我了解,这是传统的路由方法。

What do I need to do in order to make routing with options.EnableEndpointRouting = true;我需要做什么才能使用options.EnableEndpointRouting = true;进行路由options.EnableEndpointRouting = true; ? ?

Given that the redirect you are applying here does not relate directly to ASP.NET Core MVC, one option is to Use Routing Middleware instead of adding the route within MVC itself.鉴于您在此处应用的重定向与 ASP.NET Core MVC 没有直接关系,一种选择是使用路由中间件,而不是在 MVC 本身内添加路由。 Here's an example:下面是一个例子:

app.UseRouter(builder =>
{
    builder.MapGet("", context =>
    {
        context.Response.Redirect("./swagger/index.html", permanent: false);
        return Task.FromResult(0);
    });
});

The parameter passed into the UseRouter delegate ( builder ) is an instance of RouteBuilder , which is the same type that is passed into the UseMvc delegate.传递到所述参数UseRouter委托( builder )是一个实例RouteBuilder ,其是传递到相同类型UseMvc委托。 In the example above, we just apply the required route directly.在上面的例子中,我们只是直接应用所需的路由。

This call can be added either before or after your existing call to UseMvc , but adding it before might be more logical (and maybe slightly more performant) seeing as the decision to redirect can be made without involving MVC.可以在现有的UseMvc调用之前或之后添加此调用,但在之前添加它可能更合乎逻辑(并且性能可能稍高),因为可以在不涉及 MVC 的情况下做出重定向的决定。

It's worth noting that this approach doesn't use endpoint routing at all.值得注意的是,这种方法根本不使用端点路由。 In ASP.NET Core 2.2, Endpoint Routing only applies to ASP.NET Core MVC :在 ASP.NET Core 2.2 中,端点路由仅适用于 ASP.NET Core MVC

With the release of endpoint routing in ASP.NET Core 2.2, endpoint linking is limited to MVC/Razor Pages actions and pages.随着 ASP.NET Core 2.2 中端点路由的发布,端点链接仅限于 MVC/Razor Pages 操作和页面。 The expansions of endpoint-linking capabilities is planned for future releases.端点链接功能的扩展计划在未来版本中进行。

Perhaps in the near future, this routing middleware approach will also change, but for now the approach I've provided works well and is fully supported.也许在不久的将来,这种路由中间件方法也会发生变化,但就目前而言,我提供的方法运行良好并且得到了全面支持。

As an alternative to using the routing middleware, it's also possible to use a simple custom middleware function to do the redirect.作为使用路由中间件的替代方法,还可以使用简单的自定义中间件函数来执行重定向。 Here's an example of that for completeness:为了完整性,这里有一个例子:

app.Use(async (context, next) =>
{
    if (context.Request.Path == "/")
    {
        context.Response.Redirect("./swagger/index.html", permanent: false);
        return;
    }

    await next();
});

There are even more options for achieving this, but I won't enumerate them all here.实现这一目标还有更多选择,但我不会在此一一列举。

It should be worth noting that in ASP.NET Core 3.1, the following works:值得注意的是,在 ASP.NET Core 3.1 中,以下工作:

app.UseEndpoints(endpoints =>
{
    endpoints.MapGet("/", context =>
    {
        context.Response.Redirect("swagger/index.html", permanent: false);
        return Task.CompletedTask;
    });

    endpoints.MapControllers();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM