The problem is that when I define GetXXX as GetXXXX(string var1, string var2) it gives me valid data, but when I define it as GetXXXX(string var3, string var4) each param returns null.
Ok. I'm still confused. Here's the actual code snippet and this is being tested by a web request code.
routes.MapRoute(
"CreateXXXX",
"{controller}/{action}/{var1}/{var2}",
new
{
controller = "Authentication",
action = "CreateXXX"
});
routes.MapRoute(
"GetXXXX",
"{controller}/{action}/{var3}/{var2}",
new
{
controller = "Authentication",
action = "GetXXXX",
sessionIdAsString = "",
deviceId = ""
});
Code:
public JsonResult CreateXXX(string var1, string var2)
{
var1 = valid value
var 2 = valid value
}
public JsonResult GetXXXX(string var3, string var2)
{
var 3 = null ( Not a valid value )
var 2 = valid value
}
here is the code used to call it
WebRequest webRequest =
WebRequest.Create(
String.Format("http://localhost:8080/Authentication/CreateXXXX/{0}/{1}", var1,var3));
WebResponse response = webRequest.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(MyObject));
MemoryStream strm = new MemoryStream(Encoding.UTF8.GetBytes(responseFromServer));
So var 3 is always null, but in the GetXXXX if I give var3 the param name of var1 I get var3's actual value. It looks like it's because the MapRoute sees params of string, string and just grabs that and not my GetXXXX definition.
The routing does not know anything about the parameter naming as this has to be resolved in IIS first before it is even passed on to the web application itself and into your route table. Your routes have to be different.
Since you are expecting your default actions for each route, try using the following for your URL strings:
"{controller}/CreateXXX/{var1}/{var2}"
"{controller}/GetXXXX/{var3}/{var2}"
This should ensure that the right action is selected. If your only controller is the Authentication controller, you could probably replace the controller parameter as well.
Christopher Reed, MCT, MCPD, MCTS, Microsoft Specialist, MTA
"The oxen are slow, but the earth is patient."
These two routes are identical and the second one will never trigger. That is because the first route will always catch the current request due to the route format.
The first section of the route has no part in matching a URL to a route, it only designates a unique route.
This line:
"{controller}/{action}/{var1}/{var2}",
is exactly the same as :
"{controller}/{action}/{var3}/{var2}",
As mentioned earlier, the routing system doesn't look at variable names in the route, it matches the texts position in a route with the variables you specified.
But consider a url like
http://yousite/home/GetXXXX/1/2
the route is
/home /GetXXXX /1 /2 which maps to
{controller}/{action} /{var1}/{var2} or
{controller}/{action} /{var3}/{var2}
{controller} = home
{action} = GetXXXX
{var1} = 1
{var2} = 2
The routing engine is looking for a format, not variable names and both these formats are 100% indetical. The routing engine runs the rule through each route IN ORDER. meaning the first one that matches is taken.
You need to differentiate the routes by adding static text (no {}) in the url. That will mean that instead of a variable placeholder, you are looking for a specific string.
if you specify a default value, it will be added to the route values collection if the parse does not add it. the router has no idea what the route collection values are used for, it just builds a name value collection.
the second specification in the route map is the IRouteHandler (MapRoute sets it to the mvc route handler). the handlers job is to process the request. it may or may not look at the route values collection. the mvc handler does look at the route values collection
to determine the controller name, and actionresult method to call, but the static file handler does not use the route values.
As mentioned earlier, the routing system doesn't look at variable names in the route, it matches the texts
position in a route with the variables you specified.
... ...
But bruce of sqlworks says "route parameters are bound by name, not position". I am confused by the apparently conflicting statements of you two. I am also quite often confused by MVC routing configurations. Please clarify, thanks.
nick5454
Member
587 Points
486 Posts
Question about routes
Aug 07, 2012 02:45 PM|LINK
I have the following routes:
routes.MapRoute( "CreateXXXX", "{controller}/{action}/{var1}/{var2}", new { controller = "Authentication", action = "CreateXXXX", var1 = UrlParameter.Optional, var2 = UrlParameter.Optional }); routes.MapRoute( "GetXXXX", "Authentication/GetXXXX/var3/var4");The problem is that when I define GetXXX as GetXXXX(string var1, string var2) it gives me valid data, but when I define it as GetXXXX(string var3, string var4) each param returns null.
What am I doing wrong?
Careed
All-Star
18764 Points
3637 Posts
Re: Question about routes
Aug 07, 2012 04:17 PM|LINK
Your GetXXXX route does not have any parameters. Compare your two routes.
"The oxen are slow, but the earth is patient."
nick5454
Member
587 Points
486 Posts
Re: Question about routes
Aug 07, 2012 04:42 PM|LINK
routes.MapRoute( "GetXXXX", "Authentication/GetXXXX/var3/var4", new { controller = "Authentication", action = "GetXXXX", var3 = UrlParameter.Optional, var4 = UrlParameter.Optional });I tried that also and var3 is always null. How should it be changed?
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Question about routes
Aug 07, 2012 05:05 PM|LINK
routes.MapRoute( "GetXXXX", "Authentication/GetXXXX/{var3}/{var4}", new { controller = "Authentication", action = "GetXXXX", var3 = UrlParameter.Optional, var4 = UrlParameter.Optional });Blog | Twitter : @Hattan
bruce (sqlwo...
All-Star
36676 Points
5438 Posts
Re: Question about routes
Aug 07, 2012 06:11 PM|LINK
route parameters are bound by name, not position:
so
Authenication/CreateXXXX/a/b
will map to
ActionResult CreateXXX(string var2, string var1)
<div></div>ActionResult CreateXXX(string var1, string var2)
ActionResult CreateXXX(string var1, string var2, string var3) // add form field named var3
<div></div>nick5454
Member
587 Points
486 Posts
Re: Question about routes
Aug 08, 2012 12:28 PM|LINK
Ok. I'm still confused. Here's the actual code snippet and this is being tested by a web request code.
routes.MapRoute( "CreateXXXX", "{controller}/{action}/{var1}/{var2}", new { controller = "Authentication", action = "CreateXXX" }); routes.MapRoute( "GetXXXX", "{controller}/{action}/{var3}/{var2}", new { controller = "Authentication", action = "GetXXXX", sessionIdAsString = "", deviceId = "" }); Code: public JsonResult CreateXXX(string var1, string var2) { var1 = valid value var 2 = valid value } public JsonResult GetXXXX(string var3, string var2) { var 3 = null ( Not a valid value ) var 2 = valid value }here is the code used to call it
WebRequest webRequest = WebRequest.Create( String.Format("http://localhost:8080/Authentication/CreateXXXX/{0}/{1}", var1,var3)); WebResponse response = webRequest.GetResponse(); Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(MyObject)); MemoryStream strm = new MemoryStream(Encoding.UTF8.GetBytes(responseFromServer));So var 3 is always null, but in the GetXXXX if I give var3 the param name of var1 I get var3's actual value. It looks like it's because the MapRoute sees params of string, string and just grabs that and not my GetXXXX definition.
Careed
All-Star
18764 Points
3637 Posts
Re: Question about routes
Aug 08, 2012 01:45 PM|LINK
The routing does not know anything about the parameter naming as this has to be resolved in IIS first before it is even passed on to the web application itself and into your route table. Your routes have to be different.
Since you are expecting your default actions for each route, try using the following for your URL strings:
"{controller}/CreateXXX/{var1}/{var2}"
"{controller}/GetXXXX/{var3}/{var2}"
This should ensure that the right action is selected. If your only controller is the Authentication controller, you could probably replace the controller parameter as well.
"The oxen are slow, but the earth is patient."
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Question about routes
Aug 08, 2012 03:14 PM|LINK
routes.MapRoute( "CreateXXXX", "{controller}/{action}/{var1}/{var2}", new { controller = "Authentication", action = "CreateXXX" }); routes.MapRoute( "GetXXXX", "{controller}/{action}/{var3}/{var2}", new { controller = "Authentication", action = "GetXXXX", sessionIdAsString = "", deviceId = "" });These two routes are identical and the second one will never trigger. That is because the first route will always catch the current request due to the route format.
The first section of the route has no part in matching a URL to a route, it only designates a unique route.
This line:
"{controller}/{action}/{var1}/{var2}",is exactly the same as :
"{controller}/{action}/{var3}/{var2}",As mentioned earlier, the routing system doesn't look at variable names in the route, it matches the texts position in a route with the variables you specified.
But consider a url like
http://yousite/home/GetXXXX/1/2
the route is
/home /GetXXXX /1 /2 which maps to
{controller}/{action} /{var1}/{var2} or
{controller}/{action} /{var3}/{var2}
{controller} = home
{action} = GetXXXX
{var1} = 1
{var2} = 2
The routing engine is looking for a format, not variable names and both these formats are 100% indetical. The routing engine runs the rule through each route IN ORDER. meaning the first one that matches is taken.
You need to differentiate the routes by adding static text (no {}) in the url. That will mean that instead of a variable placeholder, you are looking for a specific string.
Blog | Twitter : @Hattan
bruce (sqlwo...
All-Star
36676 Points
5438 Posts
Re: Question about routes
Aug 08, 2012 03:37 PM|LINK
one thing to under stand is the router does not call the controller. its job is to parse the url into the route values collection. so with route:
"{controller}/{action}/{var1}/{var2}"
and the url a/b/c/d, the route values collection will be
routevalues["controller"] == "a"
routevalues["action"] == "b"
routevalues["var1"] == "c"
routevalues["var2"] == "d"
if you specify a default value, it will be added to the route values collection if the parse does not add it. the router has no idea what the route collection values are used for, it just builds a name value collection.
the second specification in the route map is the IRouteHandler (MapRoute sets it to the mvc route handler). the handlers job is to process the request. it may or may not look at the route values collection. the mvc handler does look at the route values collection to determine the controller name, and actionresult method to call, but the static file handler does not use the route values.
antonyliu200...
Member
168 Points
310 Posts
Re: Question about routes
Aug 08, 2012 04:07 PM|LINK
But bruce of sqlworks says "route parameters are bound by name, not position". I am confused by the apparently conflicting statements of you two. I am also quite often confused by MVC routing configurations. Please clarify, thanks.