Toran Billups
Toran Billups

Reputation: 27399

Writing a custom aspnet mvc action without an action

I'm looking to write a custom route that would allow the following

http://localhost/blog/tags/foo

Currently this is what actually works

http://localhost/tags/Index/nhibernate

I've tried the following with no success - any help would be appreciated

routes.MapRoute(
    "Tags",
    "{controller}/{id}",
    new { Controller = "Tags", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute(
    "Tags",
    "blog/{controller}/{id}",
    new { Controller = "Tags", action = "Index", id = "" }
);

Upvotes: 1

Views: 177

Answers (1)

Matthew Dresser
Matthew Dresser

Reputation: 11442

You could use in your global.asax something like this:

routes.MapRoute("Tags",
                "blog/tags/{TagName}", 
                new { Controller = "Tags", action = "ShowTag", TagName = "" });

You'd then need a controller called 'TagsController.cs' with an ActionResult method called ShowTag plus a corresponding aspx called ShowTag.aspx. Your ShowTag method should look something like this:

public ActionResult ShowTag(string TagName)
{
    //do stuff here to get Id from tag name and get other data etc...
    return View();
}

Note that the order in which you map routes in the Global.asax.cs does matter.

Upvotes: 2

Related Questions