1

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?

7
  • What is WSFederationConstants.Parameters.Action. And it needs to be return RedirectToAction("Start","Base", new { returnUrl = returnUrl }); if the value of that constant is "returnUrl" (your getting length=9 because there are 9 characters in "returnUrl") Commented May 25, 2016 at 6:06
  • as suggested by @StephenMuecke i did and am able to get the url in Base Controller. But the url fetched is not in proper format it comes with '%2f%f wsignin...' in place of /(slash) and ?(question mark) so action variable is unable to fetch the action from querystring. Commented May 25, 2016 at 7:04
  • That because the method encodes it. But what is the purpose of this? If you have a value for returnUrl, why are you not just redirecting to it using return RedirectToLocal(returnUrl); - what is your start() method for? Commented May 25, 2016 at 7:12
  • I have many parameters inside the url .Its not just action parameter alone.It has parameter to perform a sign in . Commented May 25, 2016 at 7:17
  • What difference does that make. Commented May 25, 2016 at 7:18

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I have many parameters inside the url . I have to fetch them using querystring and as suggested by stephen i am able to get the url in Base Controller. But the url fetched is not in proper format it comes with '%2f%f wsignin...' in place of /(slash) and ?(question mark) so action variable is unable to fetch the action from querystring.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.