0

I was getting the query string back using:

public ActionResult Index(int id)
{
  var queryString = Request["myQueryString"];
}

Then I looked at:

help-testing-mvc3-controller-that-accesses-querystring

Which states:

It is against MVC's design pattern to use HttpRequest directly. You can access the query string variables on your action as parameters.

I don't really understand this. Is what I've done against the design pattern? If it is why is that and how could it be done?

0

3 Answers 3

1

It breaks the concept of model binding. It also gets complicated with unit testing and trying to new up a new HttpContext for a test. If it was just a parameter, you could just pass the value.

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

Comments

1

The preferred (and easier to read) method would be:

public ActionResult Index(int id, string myQueryString)
{
    ...
}

Comments

0

Your action method should take most of the data submitted from your form. One of the strengths of MVC is the model binding it has within it. Check out this page, as it has a good example of this:

http://www.codeproject.com/Articles/159749/ASP-NET-MVC-Model-Binding-Part1

You can accept literals (string, bool, etc.) but also strongly typed objects in your action methods.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.