views:

1133

answers:

5

Hi folks,

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 programatically 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

Does anyone have any suggestions to why this would be happening?

I was under the impression that it would find the Home route but append any values not found as query string items?

cheers!

Update

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.

Update 2

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. ???????? Please help!

+1  A: 

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"});
Torkel
the reason why i can't use RedirectToRoute or RedirectToAction is because i'm trying to proramatically generate some <a href=... /> code. I don't really want to go to that page, but determine the HTML to goto that page.
Pure.Krome
+1  A: 

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.

Ben Scheirman
A: 

hmm. I was trying to get the following route results :-

http://www.mysite.com/
http://www.mysite.com/?page=2
http://www.mysite.com/?tag=foo
http://www.mysite.com/?tag=foo&amp;page=2

I didn't think it was very good to be having

http://www.mysite.com/foo/2   (tag / page)

and permutations of that. For me, i was looking at a RESTful route/url to be one that is human readable AND something i would want tp be SEO'd by a robot. the paging and tagging are value-add IMO .. while the main site (and main sections of the site) would be included in the sites sitemap.

so if i wanted to do the things above ... what should i do in the global.asax?

Pure.Krome
A: 

You should check out Phil's Route Tester:

ASP.NET Routing Debugger

David P
i've been using that and it's not helping :( It's really weird! tag is always string.empty and page is always 1 :(
Pure.Krome
A: 

i've even tried doing

route.MapRoute("theRoute", "page/{page}",  
    new { controller="Post", action="Index", page=1 });

and that is still giving me null :(

these are my last two routes....

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

// This is never called, btw :(
routes.MapRoute(
    "Home-Paged",
    "page/{page}",
    new { controller = "Post", action = "Index", page = 1 }
    );

any suggestions?

Pure.Krome