ASP.Net MVC: "Random" URLs getting generated by URL.Action

Posted by Daniel Magliola on Stack Overflow See other posts from Stack Overflow or by Daniel Magliola
Published on 2010-03-12T01:35:37Z Indexed on 2010/03/12 1:37 UTC
Read the original article Hit count: 502

I have 2 very similar routes, just because i'm trying to generate two different URLs for the same resource (same Controller/Action), and both are very similar.

These are the routes definitions:

  routes.MapRoute("Post2.Mp3", "sites/{siteSlug}/post/{brand}/aconstant-{slug}.mp3", new { controller = "Posts", action = "Mp3" }, new { siteSlug = @"[A-Za-z0-9-_]+", slug = @"[^(aconstant\-)][A-Za-z0-9-_]+", brand = @"[A-Za-z0-9-_]+" });

  routes.MapRoute("Post.Mp3", "sites/{siteSlug}/post/{slug}.mp3", new { controller = "Posts", action = "Mp3" }, new { siteSlug = @"[A-Za-z0-9-_]+", slug = @"[A-Za-z0-9-_]+" });

"brand" is going to be my site name, which is the same as "aconstant" in those routes.

Now, if I try this:

Url.Action("ShowMp3", "Posts", new { siteSlug = post.Site.Slug, slug = post.Slug, origin = "origfeedwithaudio", brand = "aconstant" })

sometimes I get the URL I expect:

/sites/site-Name/post/aconstant/aconstant-post-Name.mp3

but sometimes, I get this:

/sites/site-Name/post/post-Name.mp3?brand=aconstant

By sometimes, I mean that different sets of "slugs" give me one or the other. I haven't seen the same set of slugs give me different URLs.

I haven't found any reasonable rule for when i'm getting one or the other, it seems random.

What is going on here?
How can I be getting different URLs based on esentially the same arguments?

Thanks!
Daniel

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about routing