Reputation: 87027
I have the following one route, registered in my global.asax.
routes.MapRoute(
"Home", // Unique name
"", // Root url
new { controller = "Home", action = "Index",
tag = string.Empty, page = 1 }
);
kewl. when I start the site, it correctly picks up this route.
Now, when I try to programmatically do the following, it returns NULL.
var pageLinkValueDictionary =
new RouteValueDictionar(linkWithoutPageValuesDictionary)
{{"page", 2}};
VirtualPathData virtualPathData =
RouteTable.Routes.GetVirtualPath(viewContext, "Home"
pageLinkValueDictionary);
// NOTE: pageLinkValueDictionary ==
// Key: Action, Value: Index; Key: page, Value: 2
Why would this be happening?
I was under the impression that it would find the Home route but append any values not found as query string items?
Still no luck with this. Also, using the MVC RC, I now need to change the viewContext to veiwContext.RequestContext .. which compiles but I'm still getting a null result.
When I have the route without the page=1
default item, the route IS FOUND.
eg.
routes.MapRoute(
"Home",
"",
new { controller = "Post", action = "Index", tags = string.Empty }
);
.. and RouteTable.Routes.GetVirtualPath
returns a VirtualPathData
instance. When I add the page=1
(default value) back in, the VirtualPathData
instance returned is null?
Upvotes: 2
Views: 2978
Reputation: 41001
I think your route should be like this:
route.MapRoute("theRoute", "{controller}/{action}/{tag}/{page}",
new { controller="Post", action="Index", tag="", page=1 });
or (depending on what the full URL should look like)...
route.MapRoute("theRoute", "/{tag}/{page}",
new { controller="Post", action="Index", tag="", page=1 });
This will still match a request to http://mysite.com/ and go to your default route values defined above. But now when you specify a tag or page, you'll get the url you wanted.
Upvotes: 1
Reputation: 3404
Well the reason it returns null is because there is no route with a "page" route data.
Could you expand a little bit on what you are trying to achieve? If you want to redirect to a page with the url /page/2 or /?page=2 , then you should be using RedirectToRoute or RedirectToAction:
return RedirectToRoute("IndexDefault", new {page = "2"});
Upvotes: 1