1

In my HomeController I'm trying to get information using Request.QueryString

        string aa = Request.QueryString["aa"];
        string bb = Request.QueryString["bb"];

So In the address bar I am expecting something like:

< something >?aa=12345&bb=67890

I created a new route:

        routes.MapRoute(
            "Receive",
            "Receive",
            new { controller = "Home", action = "Index" }
        );

And I'm trying to use it in this way:
http://localhost:54321/Receive?aa=12345&bb=67890

But I'm getting the following error:

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Receive

6
  • You don't need to use Reuqest.Querysting. Use the parameters of the Action. Commented Nov 7, 2011 at 6:43
  • I'm getting this part: ?aa=12345&bb=67890 from an outside source Commented Nov 7, 2011 at 6:46
  • 2
    I think your routing is goofed which is why you are getting a 404. Please look at some tutorials, specifically here: asp.net/mvc/tutorials/asp-net-mvc-routing-overview-cs Also, like @YuriyFaktorovich says, you really shouldn't be using Request.QueryString, but rather passing those as parameters to your action method (Function Retrieve(byval aa as string, byval bb as string) as actionresult) Commented Nov 7, 2011 at 6:47
  • Thanks Tommy, I just had to put my route first and it solved all the question with the QueryString! :) Commented Nov 7, 2011 at 7:14
  • Awesome - glad that worked for you! I am going to put that as the answer, feel free to mark it when you get a chance. Commented Nov 7, 2011 at 15:38

4 Answers 4

2

I think your routing is goofed which is why you are getting a 404. Please look at some tutorials, specifically here: asp.net/mvc/tutorials/asp-net-mvc-routing-overview-cs

Also, like @YuriyFaktorovich says, you really shouldn't be using Request.QueryString, but rather passing those as parameters to your action method

Example in VB:

Function Retrieve(ByVal aa as String, ByVal bb as String) as ActionResult
Sign up to request clarification or add additional context in comments.

1 Comment

@Bojangles - yes they do. It is a language that has been around for over 20 years. VB still has a very active and large community. But, thanks for your insights.
2

You can access the Query String values in 2 ways...

  • grab the values in the controller initialization
  • use the values in your action
  • specifying the route with those variables

1 - grab the values in the controller initialization

protected override void Initialize(RequestContext requestContext) {
    // you can access and assign here what you need and it will be fired
    //  for every time he controller is initialized / call

    string aa = requestContext.HttpContext.Request.QueryString["aa"],
           bb = requestContext.HttpContext.Request.QueryString["bb"];

    base.Initialize(requestContext);
}

2 - use the values in your action

public void ActionResult Index(string aa, string bb) {
    // use the variables aa and bb, 
    //  they are the routing values for the keys aa and bb
}

3 - specifying the route with those variables

routes.MapRoute(
    "Receive",
    "Receive/{aa}/{bb}",
    new { 
        controller = "Home", 
        action = "Index", 
        aa = UrlParameter.Optional, 
        bb = UrlParameter.Optional }
);

Comments

0

Use "Receive/" for the url in the route, and don't use Request.Querystring.

You can modify your action to be

public ActionResult Index(string aa, string bb) {...}

The ASP.Net MVC framework will hydrate those items for you.

Comments

0

Your HTTP 404 error is because your new route is very likely in the wrong place. Make sure your new route is before the default route:

routes.MapRoute(
        "Receive",
        "Receive",
        new { controller = "Home", action = "Index" }
    ); 

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

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.