2

I was wondering how I can access the GET parameter in url's like ?returnTo=url

I'm using MVC3 with C# and would like to get the value in a Controller. I've snooped around in the Request object, which has the values I need in the "Query" property.

Do I have to parse that QueryString manually or is there an easier way?

5 Answers 5

6

No, you don't have to parse manually. MVC3 uses model binding automatically. It means that if you add returnTo string parameter to your action method, MVC will automatically extract value from query string and initialize your action parameter. Default model binder tries to extract parameter values from Request.QueryString, Request.Form, RouteData. You can override or change part of behaviour if you implement custom model binder or register custom value provider. For more info take a look at Model Binding

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

Comments

5
string url = Request.QueryString["returnTo"]

1 Comment

there's no school like the old school!!
2

You can still use Request.QueryString["paramname"].

More information can be found here: http://weblogs.asp.net/imranbaloch/archive/2011/02/19/understanding-request-validation-in-asp-net-mvc-3.aspx

Comments

1

Like this:

public ActionResult(string returnTo) 
{
     return Content("Return to is: " + returnTo);
}

Comments

0

This is a simple introduction to ASP.NET MVC Controllers. There is more than one way to access form posted data. This tutorial will show you how you how.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.