简体   繁体   中英

Unable to fetch returnurl in asp.net mvc Controller

I have two controllers Base and Login.

Base Controller:

public ActionResult start()
    {
       string action = Request.QueryString[WSFederationConstants.Parameters.Action];
    }

Login Controller:

 public ActionResult Login(string user,string password,string returnUrl)
    {
        if (FormsAuthentication.Authenticate(user, password))
        {

            if (string.IsNullOrEmpty(returnUrl) && Request.UrlReferrer != null)
                returnUrl = Server.UrlEncode(Request.UrlReferrer.PathAndQuery);

           return RedirectToAction("Start","Base", returnUrl });
        }
        return View();
    }

After authentication is done it gets redirected to Start action in Base Controller as expected. But the querystring doesnot fetch the value. When hovered over the querystring it shows length value but not the uri.

How to use the url sent from Login controller in Base Controller and fetch parameters from it?

You are actually returning a 302 to the client. From the docs .

Returns an HTTP 302 response to the browser, which causes the browser to make a GET request to the specified action.

When doing that the client will make another request with the url that you created. In your case something like youruri.org/Base/Start . Take a look at the network tab in your browser (F12 in Chrome).

What I think you want to do is:

return RedirectToAction
  ("Start", "Base", new { WSFederationConstants.Parameters.Action = returnUrl  });

Assuming that WSFederationConstants.Parameters.Action is a constant. If WSFederationConstants.Parameters.Action returns the string fooUrl your action will return the following to the browser:

Location:/Base/Start?fooUrl=url
Status Code:302 Found

Another option is to actually pass the value to the controller:

public class BaseController: Controller
{
    public ActionResult start(string myAction)
    {
       string localAction = myAction; //myAction is automatically populated.
    }
}

And in your redirect:

return RedirectToAction
  ("Start", "Base", new { myAction = returnUrl  });

Then the BaseController will automatically fetch the parameter, and you don't need to fetch it from the querystring.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM