1

I have a controller called Search. A normal url would be the following:

  • localhost:44351/<ClientName>/Search/ByCity

This would hit my ByCity action within my SearchController.

Now however, a url such as the following example, would also need to hit an action within the SearchController:

  • localhost:44351/<ClientName>/Search/Pharmacy/ByCity

I need to somehow tell my SearchController, if the url contains "Pharmacy/ByCity", to go to the ByCity action.

I've tried using the routing attribute, but my app still hits my old Pharmacy action instead.

In my RouteConfig, I have this:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapMvcAttributeRoutes();   
}

Then, in my SearchController, I have this:

public virtual ActionResult Pharmacy()
{
    //this is an existing action, which gets hit, even when I type in "Pharmacy/ByCity", which is not what I want to happen.
}

[Route("Pharmacy/ByCity")]
public virtual ActionResult ByCity()
{
    //this never gets hit
}

Any idea how to have a url containing "Pharmacy/ByCity" to hit my "ByCity" action, rather than "Pharmacy"?

Thanks

3
  • What is ClientName means? Is this just constant string or this is parameter with a variable value, that might take different values like clientname1, clientname2, etc. Commented Mar 15, 2022 at 3:29
  • @Jackdaw, "Clientname" is a variable value. So it could have different values such as clientname1 & clientname2 Commented Mar 15, 2022 at 5:24
  • Okay. I have updated routing in my answer below by using the conventional route set up, if this solution is acceptable for you. Commented Mar 15, 2022 at 5:33

2 Answers 2

1

It is possible to achieve with the conventional route by set up like below:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
    name: "Pharmacy",
    url: "{clientname}/{controller}/Pharmacy/{action}",
    defaults: new { controller = "search" }
);

routes.MapRoute(
    name: "Search",
    url: "{clientname}/{controller}/{action}",
    defaults: new { controller = "search", action = "Index" }
);          

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this worked for what I needed :)
1

Routes are accessed depending on their Order in the routing table.

For conventional routing (RouteConfig.cs), you could add your specific route before the default route.

  1. Remove your Route[] attributes in the controller
  2. Use the code below for RouteConfig
public static void RegisterRoutes(RouteCollection routes)
{
   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

   // add your specific route, before the default route
   routes.MapRoute(
      name: "SearchByCity", // random name
      url: "Search/Pharmacy/ByCity",
      defaults: new { controller = "Search", action = "ByCity" }
   );
   
   // this is the default route
   routes.MapRoute(
      name: "Default",
      url: "{controller}/{action}/{id}",
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
   );
}

If you want to use Attribute Route, follow steps below.

  1. Remove the default route in RouteConfig.
public static void RegisterRoutes(RouteCollection routes)
{
   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
   routes.MapMvcAttributeRoutes();
}
  1. Then use the controller below, we used RoutePrefix for the controller, and Route for the actions.
[RoutePrefix("Search")]
public class SearchController : Controller
{
   [Route("Pharmacy")]
   public virtual ActionResult Pharmacy()
   {
      return View("index");
   }

   [Route("Pharmacy/ByCity")]
   public virtual ActionResult ByCity()
   {
      return View("index");
   }
}

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.