tags:

views:

86

answers:

2

This is more of a question to satisfy my curiosity vs something I really need answered. Back in ASP.NET WebForms, I'd occasionally use a positional parameter in a query string if I only had to pass one thing to a page. For example:

http://localhost/site/MyPage.aspx?ABCD1234

Then my code would look like this:

string accountNumber = "";
if (Request.QueryString.Count > 0)
   accountNumber = Request.QueryString[0];

In MVC, can you pass a positional query string parameter to a controller method instead of accessing it through Request.QueryString?

A: 

As long as your controller parameter is a string, then you can call it by not even naming the parameter and just appending it to the URI.

http://mydomain.com/controller/action/SingleStringParameterValue

If you want to use it as a query string parameter, then you have to have matching names on the controller and query string variable I believe.

NickLarsen
I know I can do that as part of the URL, just curious if it can be done as part of the query string.
Pete Nelson
A: 

You can do the same thing with MVC as well. You just have to make sure you don't have a route that directs the url with the query string to a different action method.

This works from the default project that ASP.NET MVC creates when you new up a project:

    public ActionResult Index() {

        if (Request.QueryString.Count > 0) 
            ViewData["Message"] = "Welcome to ASP.NET MVC: " + Request.QueryString[0];
        else
            ViewData["Message"] = "Welcome to ASP.NET MVC";

        return View();
    }
Matt Spradley
yes but that isnt exactly what he is asking from my understanding. He wants to know if its possible to use the variable name in the first position of the query string as the parameter passed to the controller action, and the answer is likely no.
NickLarsen
You'r right NickLarsen. I changed my answer to reflect the question.
Matt Spradley