Sunday, 15 January 2012

another asp.net routing issue -



another asp.net routing issue -

i searched around problem, not find solution...

i have blogcontroller, , want match next routes separate action:

/blog/ /blog/rss /blog/tags/tagname

however, want match other url's, like:

/blog/my-post /blog/other-post

to post action.

i tried

routes.maproute("blog", "blog/{action}/{param}", new { controller = "blog", action = "index", param = urlparameter.optional }); routes.maproute("blogpost", "blog/{slug}", new { controller = "blog", action = "post" });

but sec route never matched.

any ideas?

the first route allready match urls in form of blog/slug.

whe resolving routes, asp.net mvc seek utilize first match, , if there no action method. asp.net mvc's routing still won't seek next route.

so routes, url blog/my-first-article match first url , mvc my-first-action method on blogcontroller class.

solution 1

you either define seperate routes each method, this:

routes.maproute("blog index", "blog", new { controller = "blog", action = "index" }); routes.maproute("blog rss feed", "blog/rss", new { controller = "blog", action = "rss" }); routes.maproute("posts tag", "blog/tags/{params}", new { controller = "blog", action = "tags" }); routes.maproute("blogpost", "blog/{slug}", new { controller = "blog", action = "post" });

solution 2

you utilize constraint define valid values {action} in first route.

routes.maproute("blog", "blog/{action}/{param}", new { controller = "blog", action = "index", param = urlparameter.optional }, new { action = 'index|rss|tags' }); routes.maproute("blogpost", "blog/{slug}", new { controller = "blog", action = "post" });

the constraint in form of regex.

asp.net-mvc asp.net-mvc-routing

No comments:

Post a Comment