1

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 }
            );
        }
    }
2
  • Less precise routes should normally be last. BlogSearchRoute is never used when it is after Default route. Commented Jun 29, 2015 at 15:18
  • Side notes: search text generally does not work as part of "path" segment of URI due to restrictions on characters allowed there; consider to specify explicit urls explicitly to minimize number of conflicts in the routing table: url: "blog/search/{searchTag}". Not directly related to your problem. Commented Jun 29, 2015 at 15:26

3 Answers 3

1

In your custom route, define your searchTag as optional using:

routes.MapRoute(
    name: "BlogSearchRoute",
    url: "{controller}/{action}/{searchTag}",
    defaults: new { 
         controller = "Blog", 
         action = "Search",
         searchTag = UrlParameter.Optional
    }
);

UPDATE

You should define your Default route at the bottom under all your custom routes.

Sign up to request clarification or add additional context in comments.

7 Comments

That doesn't solve the problem... parameter is still null.
And what if you remove the attribute [Route("blog/search/{searchTag:string}")] ? Because now you define the routes in your routings and as an attribute in your controller
Still doesn't work. See the update, maybe that would help.
Can you add your entire routeconfig.cs content?
You have to place your default route at the bottom, now it will always use this route instead of your other custom routes
|
0

I managed to get it working by removing BlogSearchRoute, the [Route()] attribute from my method and renaming searchTag to id

Comments

0

Your route parameter {searchTag:string} has an incorrect constraint type. The ":string" constraint type does not exist and you should either remove the constraint type or take one of the valid ones that can be found in route constraints documentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.