Elmah陷入默认路线?

时间:2015-01-23 10:54:21

标签: asp.net-mvc elmah elmah.mvc

我将Elmah.MVC添加到我的MVC网站,但是当访问/ elmah时,我的网站陷入重定向循环/ elmah?culture = en - 我认为这是因为我的一个路由配置条目:

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

            routes.MapRoute(
                "Default",
                "{culture}/{controller}/{action}/{id}",
                new { culture = CultureHelper.GetDefaultCulture(), controller = "Home", action = "SetCulture", id = UrlParameter.Optional }
            );
        }
    }

如何修改RouteConfig以使/ elmah正确显示?还是我看错了地方?

谢谢: - )

1 个答案:

答案 0 :(得分:2)

它是由名为CultureAwareControllerActivator的类实现的,它实现了IControllerActivator

我的知识有限,但似乎这个类(我自己添加)在激活控制器时连接到请求,并决定是否修改和重定向。

public class CultureAwareControllerActivator : IControllerActivator
{
    public IController Create(RequestContext requestContext, Type controllerType)
    {
        string cultureName = requestContext.RouteData.Values["culture"] as string;

        // Attempt to read the culture cookie from Request
        if (cultureName == null)
            cultureName = requestContext.HttpContext.Request.UserLanguages != null && requestContext.HttpContext.Request.UserLanguages.Length > 0
                ? requestContext.HttpContext.Request.UserLanguages[0]
                : null; // obtain it from HTTP header AcceptLanguages

        // Validate culture name
        cultureName = CultureHelper.GetImplementedCulture(cultureName); // This is safe


        if (requestContext.RouteData.Values["culture"] as string != cultureName && (string) requestContext.RouteData.Values["controller"] != "Elmah")
        {

            // Force a valid culture in the URL
            requestContext.RouteData.Values["culture"] = cultureName.ToLowerInvariant(); // lower case too

            // Redirect user
            requestContext.HttpContext.Response.RedirectToRoute(requestContext.RouteData.Values);
        }


        // Modify current thread's cultures            
        Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
        Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

        return DependencyResolver.Current.GetService(controllerType) as IController;
    }
}