Redirecting while using PathBase

回眸只為那壹抹淺笑 提交于 2021-01-29 07:12:45

问题


I have defined PathBase like

new PathString("/test_environment/");

Then I'm at https://localhost:5001/test_environment/Login

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/

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

MyMethod in MyController has an [Route("/")] attribute


回答1:


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

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

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


来源:https://stackoverflow.com/questions/54087837/redirecting-while-using-pathbase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!