Is it possible to know in which routes.Map () the call of an Action crashed? ASP.NET MVC5

2

Then #

I have two routes.MapRoute()

routes.MapRoute(
    "Home",
    "Image/{id}",
    new
    {
        controller = "Home",
        action = "Image",
        id = UrlParameter.Optional
    },
    new { id = @"\w+" }
);

routes.MapRoute(
         name: "Default",
         url: "{controller}/{action}/{id}",
         defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

And an Action

 public ActionResult Image(string id)
    {
        Debug.WriteLine(id);
        return View();

    }

Both Map.Route() will work, but in that case it would fall into the first option (because both are correct but the one that comes first is used), but if Map.Route() called "Home" did not work, I would not know in which Map.Route() would be falling when I call Action "Image"

    
asked by anonymous 18.08.2017 / 18:03

1 answer

2

Yes, you can access route data since your controller inherits from Controller or ApiController

public ActionResult Image(string id)
{
    //Caso seja ApiController
    var route = this.ControllerContext.RouteData.Route.RouteTemplate;
    Console.WriteLine(route);

    //Caso seja apenas Controller
    var route = this.ControllerContext.RouteData.Route as Route;
    Console.WriteLine(route.Url);
}

The result in my case is as: "api/{controller}/{action}"

    
18.08.2017 / 19:18