使用PathBase时重定向 [英] Redirecting while using PathBase

查看:65
本文介绍了使用PathBase时重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义了 PathBase

新的PathString("/test_environment/");

然后我在 https://localhost:5001/test_environment/Login

,登录后它将重定向到/MyController/

and after login it redirects to /MyController/

一切正常-到达控制器及其方法并正确提供数据,但是我在浏览器中的网址是:

Everything works properly - the controller and its method is reached and serves data properly, but my url in browser is:

https://localhost:5001/MyController/

工作正常,但是/test_environment/去哪里了?

It works fine, but where did /test_environment/ go?

MyMethod 有一个 [Route("/")] 属性

推荐答案

对于 app.UsePathBase ,它添加了一个中间件,该中间件从请求路径中提取指定的路径基础,并将其追加到请求路径基础中.

For app.UsePathBase, it adds a middleware that extracts the specified path base from request path and postpend it to the request path base.

对于 [Route("/")] ,如果您使用 RedirectToAction ,它将生成请求 https://localhost:5001/.

For [Route("/")], it will generate request https://localhost:5001/ if you use RedirectToAction.

您得到了 https://localhost:5001/MyController/,我假设您使用 Redirect 来重定向url.

You got https://localhost:5001/MyController/, I assume you use Redirect to redirect the url.

在下面的登录中尝试使用以下代码.

Try code below in Login.

public IActionResult Login()
{
    if (Request.Path.StartsWithSegments("/test_environment"))
    {
        return Redirect("/");
    }
    else
    {
        return Redirect("/test_environment/");
    }
}

这篇关于使用PathBase时重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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