问题
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