Redirect (relativeUrl) redirect to the wrong path in IIS

In normal AccountControllerMVC3 in my application, if installed returnUrl(in my case, I install it manually), it calls Redirect(returnUrl).

Suppose my return URL is /Admin/HealthCheck(actually this). When I debug, I get the URL, for example http://localhost:3279/Admin/HealthCheck, from a redirect call.

Then I deployed the application to http://localhost/Test. In this case, it Redirect(returnUrl)redirects me to http://localhost/Admin/HealthCheck, rather than to the expected http://localhost/Test/Admin/HealthCheck.

What's going on here? How to fix it (if it is fixed)?

The following is a snippet from the (standard) MVC3 AccountController; you can see where I get the return URL from the query string (for example http://localhost/Test/LogOn?ReturnUrl=/Admin/HealthCheck, albeit an encoded 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.");
                }
            }
+5
4

( )

, , hardcoded url, /Admin/HealthCheck URL- URL-, Url.Action("HealthCheck", "Admin"), , .

LogOn . , = > url, . , URL-.

: ASP.NET MVC URL- URL-. .

+2

Test URL- ReturnUrl Test/Admin/HealthCheck.

MSDN Controller.Redirect():

RedirectResult, URL.

, "/Admin/HealthCheck" , , .

+1

Request.UrlReferrer.AbsoluteUri

[HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        string refUri = Request.UrlReferrer.AbsoluteUri;

        if (ModelState.IsValid)
        {
            if (Membership.ValidateUser(model.UserName, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    return Redirect(refUri + "#/account/login");
                }
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }
+1

www.domain.com/Test/Admin/HealthCheck, , URL- .

If you use Visual Studio, you can change the virtual path assigned to your project by clicking on the project in the solution explorer and pressing F4. This will display the project properties window, which has the ability to change the virtual path. In your case, you want to change it to /Test. You still have to change the URLs to contain /Test, though.

Although, as Darin pointed out, you should use URL assistants.

0
source

All Articles