Arthur
Arthur

Reputation: 1760

How to parse query string MVC3

I have an url like this:

http://localhost:9562/Account/LogOn?ReturnUrl=%2fCabinet%2fCabinet

I need to parse it to this:

Cabinet/Cabinet

I've looked through this and this but i can't understand how to use it with my example.

Upvotes: 1

Views: 3064

Answers (2)

Samiey Mehdi
Samiey Mehdi

Reputation: 9414

Try this:

 string r = Request.QueryString["ReturnUrl"].Substring(1);
 Response.Write(r);

Upvotes: 1

p.s.w.g
p.s.w.g

Reputation: 148980

The easiest way would be to accept it as a parameter in your LogOn action:

public class AccountController : Controller
{
    public ActionResult LogOn(string ReturnUrl = "")
    {
    }
}

Note, providing a default value (i.e. = "") allows the action to execute even if the query parameter isn't present in the request.

Alternatively, you could access it through the Request property of your controller:

public class AccountController : Controller
{
    public ActionResult LogOn()
    {
        string request = this.Request.QueryString["ReturnUrl"];
    }
}

Upvotes: 9

Related Questions