alexbeletsky / elmah-mvc

Painless integration of ELMAH into ASP.NET MVC application
http://nuget.org/packages/Elmah.MVC
Apache License 2.0
266 stars 61 forks source link

Routes should not register in RouteDirection.UrlGeneration direction #85

Open rjgotten opened 8 years ago

rjgotten commented 8 years ago

The "Elmah.Mvc" and "Elmah.Mvc.Detail" routes registered in the package's Bootstrap currently run for both RouteDirection.IncomingRequest and RouteDirection.UrlGeneration.

These routes should not be used for url generation!

They are registered without any kind of constraint limiting their applicability and thus can match any url generation request from the ASP.NET MVC application into which the Elmah.Mvc package is installed.

There are real-life scenarios in which Elmah.Mvc registers its routes before the application itself and in those particualr cases, the routes from the package will overtake all of the application's routes, causing url generation to completely break down into nonsense: redirecting everything to a /elmah?<query> url, where <query> is a query string containing all the serialized route values that should've been used to matched against one of the application's own routes.

rjgotten commented 5 years ago

Wow... Has it been almost 3 years already without reply? (Nice...)

Ok then: in the event anyone else runs across this as a problem, and the developers of this package continue to not supply a solution; you can work around it by modifying the routes that the package registers, after the fact:

private static void HotPatchElmahMvc(RouteCollection routes)
{
  Patch(routes["Elmah.Mvc"]);
  Patch(routes["Elmah.Mvc.Detail"]);

  void Patch(RouteBase routeBase)
  {
    if (routeBase is Route route)
    {
      route.Constraints.Add(
        Guid.NewGuid().ToString(),
        new DirectionConstraint(RouteDirection.IncomingRequest)
      );
    }
  }
}

Here DirectionConstraint is a very simple IRouteConstraint that simply returns false on route directions that don't match up.