Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After adding ELMAH and changing elmah.mvc.route the old route is still available

I've added ELMAH to my ASP.NET MVC 4 .Net 4 web application.

The intergration was simple and it works well.

I've changed the "elmah.mvc.route" value in the app settings of my web.config to an "Admin/SiteLog" route - the elmah log is displayed at this route now

But, it is also still shown at "/elmah" for some reason (with no css styling, but same content).

How can I disable the default elmah route?

The integration was made using Elmah.MVC nuget package

like image 644
Adam Tal Avatar asked Feb 14 '13 14:02

Adam Tal


2 Answers

I've just been working through this problem myself and the latest version seems to have a couple of app settings that works nicely enough for this.

<add key="elmah.mvc.IgnoreDefaultRoute" value="true" />
<add key="elmah.mvc.route" value="admin/elmah" />

It's probably also worth being aware of the others so take a look after a default install.

<add key="elmah.mvc.disableHandler" value="false" />
<add key="elmah.mvc.disableHandleErrorFilter" value="false" />
<add key="elmah.mvc.requiresAuthentication" value="false" />
<add key="elmah.mvc.allowedRoles" value="*" />
<add key="elmah.mvc.allowedUsers" value="*" />
like image 83
willsaunders Avatar answered Sep 17 '22 15:09

willsaunders


This happens because the default route (assuming you have one) will still match to Elmah.Mvc.ElmahController.

routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional });

The "{controller}" portion of the route will find a matching controller whether you want it to or not. This is obviously problematic in this case.

You can add constraints on your routes by using IRouteConstraint, outlined here. The NotEqual constraint is actually pretty useful.

using System;
using System.Web;
using System.Web.Routing;

public class NotEqual : IRouteConstraint
{
    private string _match = String.Empty;

    public NotEqual(string match)
    {
        _match = match;
    }

    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        return String.Compare(values[parameterName].ToString(), _match, true) != 0;
    }
}

So then exclude ElmahController from the default route by using the following.

routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        new { controller = new NotEqual("Elmah") });

This will make requests for "/elmah" return a 404.

like image 37
pdubs Avatar answered Sep 18 '22 15:09

pdubs