1

I need to build a controller action to handle this pattern:

example.com/aString

where aString can be any of a set of arbitrary strings. The controller would cycle thru each of the possible values and, if none match, redirect to a 404.

I'd think it's just a matter of recoding the catch-all but am so far coming up blank. Currently using Sherviniv's suggestion:

//Catchall affiliate shortcuts.
routes.MapRoute(
   name: "affLanding",
   url: "{query}",
   defaults: new
   {
       controller = "Home",
       action = "MatchString"
   }
);

Controller:

public ActionResult MatchString(string query)
{
    _logger.Info("affLanding: " + query);
    return View();
}

If i hard-code my 'search' string into the route.config things work:

 routes.MapRoute(
       name: "search",
       url: "aString",
       defaults: new { controller = "home", action = "MatchString"}
        );
justSteve
  • 5,444
  • 19
  • 72
  • 137

1 Answers1

1

In routeconfig

  routes.MapRoute(
                 name: "Controller1",
                 url: "Controller1/{action}/{id}",
                 defaults: new { controller = "Controller1", action = "Index", id = UrlParameter.Optional }
            );
      routes.MapRoute(
                 name: "Controller2",
                 url: "Controller2/{action}/{id}",
                 defaults: new { controller = "Controller2", action = "Index", id = UrlParameter.Optional }
            );
//Other controllers
  routes.MapRoute(
            name: "search",
            url: "{query}",
            defaults: new
            {
                controller = "Home",
                action = "MatchString"
            }
        );
        routes.MapRoute(
                        name: "Default",
                        url: "",
                        defaults: new
                        {
                            controller = "Home",
                            action = "Index"
                        }
     );

In your Controller

 public ActionResult Index()
 {
  reutrn view();
 }

 public ActionResult MatchString(string query)
 {
 if(Your Condition)
 {
 //when string query doesnt find
  throw new HttpException(404, "Some description");
 }
  else
   {
     return view(Your model);
   }
 }

Remember to add all of your controller's name because if you don't mention them in route config how the server can know it is search parameter or Not. Hope it helps

Shervin Ivari
  • 1,759
  • 5
  • 20
  • 28
  • Thanks but alas produces a runtime error: The route template '/{query}' on the action named 'foo' on the controller named 'bar' cannot begin with a forward slash. – justSteve Jul 19 '19 at 01:22
  • @justSteve check out the new answer. I find the solution – Shervin Ivari Jul 19 '19 at 18:31
  • Appreciating the effort but still getting: Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. – justSteve Jul 22 '19 at 10:00
  • @justSteve I've just checked it again.it is working.what is the URL after you receiving not found the error? – Shervin Ivari Jul 22 '19 at 10:15
  • Do you follow priorities on your route config?it must be as same as mine – Shervin Ivari Jul 22 '19 at 10:16
  • In terms of 'priorities', yes, I have the search route above the default. Is it possible that my case is being complicated by the presence of other routes where the URL is hard-coded? e.g. routes.MapRoute( "express", url: "express", defaults: new { controller = "Cart", action = "Express", id = 0 }); – justSteve Jul 22 '19 at 10:36
  • I found a conflicting route, removed it and things work now. Highly appreciate the persistence. – justSteve Jul 22 '19 at 10:51