Jefferson
Jefferson

Reputation: 69

Conflicting MVC RouteConig

I have to add a new route in my RouteConfig.cs but it is only working if I remove one of the current route. Is there anyway around this? Is there a better way to do this ? should it key off of the RouteCollection?

Current

routes.MapHttpRoute("FhirSearch", "fhir/{type}", new { controller = "Fhir", action = "Search" });

New

routes.MapHttpRoute("FhirDiseasesList", "fhir/DiseasesList", new { controller = "Fhir", action = "DiseasesList" });

This also Conflicting

routes.MapHttpRoute("FhirDiseasesList", "fhir/Diseases/List", new { controller = "Fhir", action = "DiseasesList" });

Upvotes: 1

Views: 39

Answers (1)

Jerdine Sabio
Jerdine Sabio

Reputation: 6185

but it is only working if I remove one of the current route

I think both of your route works fine but what happens is the routing engine executes the route that is indicated first. So-- whichever route comes first that matches your url, that is the one that's going to be executed.

  1. I think the easiest fix is to really change the route url. This depends entirely on you but make sure they don't have matching/clashing routes.

Like separate the word diseases and list in your new route.

routes.MapHttpRoute("FhirSearch", "fhir/Diseases/List", new { controller = "Fhir", action = "DiseasesList" });
  1. If you really want to retain both routes, what you could do is-- in your Search action method, add a check if type == "DiseasesList" then redirect.
public ActionResult Search(string type)
{
   if(type == "DiseasesList"){
      return redirectToAction("DiseasesList");
   }

   ... // do something else
}

EDIT: OP mentioned without the specific routing, it would hit Search action

For routes.MapHttpRoute("FhirRead", "fhir/{type}/{id}", new { controller = "Fhir", action = "Read" }); you could add a constraint to only match id with integers.

routes.MapHttpRoute(
   "FhirRead",
   "fhir/{type}/{id}",
   new { controller = "Fhir", action = "Read" },
   new {id = @"\d+" } // routing constraint to only match id with integers
);

Upvotes: 1

Related Questions