Reputation: 4249
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
Upvotes: 1
Views: 6658
Reputation: 39807
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
Upvotes: 2
Reputation: 26690
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
);
Upvotes: 0
Reputation: 75073
You can access the Query String values in 2 ways...
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 }
);
Upvotes: 2
Reputation: 68667
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.
Upvotes: 0