I have the following method in my BlogController:
[HttpGet]
[Route("blog/search/{searchTag:string}")]
public ActionResult Search(string searchTag) {
// Doing some search
}
I want my url to be for example blog/search/programming and this should get me to a page showing only the posts that are tagged with programming
I also have the following route:
routes.MapRoute(
name: "BlogSearchRoute",
url: "{controller}/{action}/{searchTag}",
defaults: new {
controller = "Blog",
action = "Search"
}
);
Unfortunately my parameter doesn't map correctly and it is always null.
UPDATE
Additional information: here is my RouteConfig class:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes) {
routes.LowercaseUrls = true;
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("elmah.axd");
routes.MapRoute(
name: "BlogSearchRoute",
url: "{controller}/{action}/{searchTag}",
defaults: new { controller = "Blog", action = "Search", searchTag = UrlParameter.Optional });
routes.MapRoute(
name: "BlogRoute",
url: "{controller}/{action}/{id}/{title}",
defaults: new { controller = "Blog", action = "Post", title = UrlParameter.Optional});
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
BlogSearchRouteis never used when it is afterDefaultroute.url: "blog/search/{searchTag}". Not directly related to your problem.