gpt4 book ai didi

asp.net-mvc-3 - 重定向(relativeUrl)重定向到 IIS 中的错误路径

转载 作者:行者123 更新时间:2023-12-04 15:25:44 25 4
gpt4 key购买 nike

在通常 AccountController在我的 MVC3 应用程序中,如果 returnUrl设置(在我的情况下,我手动设置),它将调用 Redirect(returnUrl) .

假设我的返回 URL 是 /Admin/HealthCheck (确实如此)。当我调试时,我得到一个类似 http://localhost:3279/Admin/HealthCheck 的 URL。从重定向调用。

然后,我将我的应用程序部署到 http://localhost/Test .在这种情况下,Redirect(returnUrl)将我重定向到 http://localhost/Admin/HealthCheck而不是预期的 http://localhost/Test/Admin/HealthCheck .

这里发生了什么?我该如何解决这个问题(如果可以解决的话)?

下面是(标准)MVC3 AccountController 的一个片段;您可以看到我从查询字符串中获取返回 URL 的位置(例如 http://localhost/Test/LogOn?ReturnUrl=/Admin/HealthCheck ,尽管 URL 已编码)。

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
returnUrl = Request.Params["ReturnUrl"];

FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}

最佳答案

(in my case, I set it manually)



您实际上还没有展示这个手动设置是如何发生的,但是如果您硬编码了一个 url,例如 /Admin/HealthCheck而不是使用 url helper 来生成这个 url,例如 Url.Action("HealthCheck", "Admin")不要指望奇迹会发生。

您的 LogOn很好。它做它应该做的 => 它重定向到作为参数传递的 url。您的问题在于您设置此网址的方式。

结论:在 ASP.NET MVC 应用程序中处理 url 时总是使用 url helper。永远不要对它们进行硬编码。

关于asp.net-mvc-3 - 重定向(relativeUrl)重定向到 IIS 中的错误路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9473058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com