MVC 4路由中的大写控制器返回404

时间:2013-12-18 17:28:14

标签: asp.net-mvc routes

当我们在生产服务器“/帐户/登录”上请求此网址时,我们收到404页面未找到错误,但“/ account / Login”工作正常。只有控制器第一个字母的情况不同。

此外,我们的开发服务器上没有此问题。

问题刚出现在最后几天,但我们没有看到路线配置发生任何变化。所以我不知道是什么原因导致了这个问题。

如果您看到可以解释的内容,那么这是RouteConfig.cs代码:

using System;
using System.Globalization;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MobileWebsite.App_Start
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            if (routes == null) throw new ArgumentNullException("routes");

            const string defautlRouteUrl = "{controller}/{action}/{id}";
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            RouteValueDictionary defaultRouteValueDictionary = new RouteValueDictionary(
                    new { controller = "Home", action = "Index", id = UrlParameter.Optional });

            Route defaultRoute = new Route(defautlRouteUrl, defaultRouteValueDictionary, new MvcRouteHandler());
            routes.Add("DefaultGlobalisedClientSiteLang", new GlobalisedRouteClientSiteLang(defaultRoute.Url, defaultRoute.Defaults));
            routes.Add("DefaultGlobalisedClientSite", new GlobalisedRouteClientSite(defaultRoute.Url, defaultRoute.Defaults));
            routes.Add("DefaultGlobalised", new GlobalisedRoute(defaultRoute.Url, defaultRoute.Defaults));
            routes.Add("Default", new Route(defautlRouteUrl, defaultRouteValueDictionary, new MvcRouteHandler()));          
        }
    }

    public class GlobalisedRoute : Route
    {
        public const string CultureKey = "culture";
        static string CreateCultureRoute(string unGlobalisedUrl)
        {
            return string.Format(CultureInfo.InvariantCulture, "{{" + CultureKey + "}}/{0}", unGlobalisedUrl);
        }
        public GlobalisedRoute(string unGlobalisedUrl, RouteValueDictionary defaults) : 
            base(CreateCultureRoute(unGlobalisedUrl),
               defaults,
               new RouteValueDictionary(new { culture = new CultureRouteConstraint() }),
               new GlobalisationRouteHandler())
        {
        }
    }
    public class GlobalisedRouteClientSiteLang : Route
    {
        public const string CultureKey = "culture";
        public const string ClientSiteIdKey = "clientSiteId";
        static string CreateClientSiteRoute(string unGlobalisedUrl)
        {
            return string.Format(CultureInfo.InvariantCulture, "{{" + ClientSiteIdKey + "}}/{{" + CultureKey + "}}/{0}", unGlobalisedUrl);
        }
        public GlobalisedRouteClientSiteLang(string unGlobalisedUrl, RouteValueDictionary defaults) :
            base(CreateClientSiteRoute(unGlobalisedUrl),
               defaults,
               new RouteValueDictionary(new { culture = new CultureRouteConstraint(), clientSiteId = new ClientSiteIdRouteConstraint() }),
               new GlobalisationRouteHandler())
        {
        }
    }

    public class GlobalisedRouteClientSite : Route
    {
        public const string ClientSiteIdKey = "clientSiteId";
        static string CreateClientSiteRoute(string unGlobalisedUrl)
        {
            return string.Format(CultureInfo.InvariantCulture, "{{" + ClientSiteIdKey + "}}/{0}", unGlobalisedUrl);
        }
        public GlobalisedRouteClientSite(string unGlobalisedUrl, RouteValueDictionary defaults) :
            base(CreateClientSiteRoute(unGlobalisedUrl),
               defaults,
               new RouteValueDictionary(new { clientSiteId = new ClientSiteIdRouteConstraint() }),
               new GlobalisationRouteHandler())
        {
        }
    }

    public class GlobalisationRouteHandler : MvcRouteHandler
    {
        string CultureValue
        {
            get
            {
                return (string)RouteDataValues[GlobalisedRoute.CultureKey];
            }
        }

        RouteValueDictionary RouteDataValues
        {
            get; 
            set;
        }

        protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            if (requestContext == null) throw new ArgumentNullException("requestContext");

            RouteDataValues = requestContext.RouteData.Values;
            CultureManager.SetCulture(CultureValue);
            return base.GetHttpHandler(requestContext);
        }
    }

    public class CultureRouteConstraint : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            if (values == null) throw new ArgumentNullException("values");

            if (!values.ContainsKey(parameterName))
                return false;
            string potentialCultureName = (string)values[parameterName];
            return CultureFormatChecker.FormattedAsCulture(potentialCultureName);
        }
    }

    public class ClientSiteIdRouteConstraint : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            if (values == null) throw new ArgumentNullException("values");

            if (!values.ContainsKey(parameterName))
                return false;
            string potentialClientSiteId = (string)values[parameterName];
            return ClientSiteIdFormatChecker.FormattedAsClientSiteId(potentialClientSiteId);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

事实证明这是ViewEngine缓存机制中的一个错误,请参阅此处的说明: http://blogs.msdn.com/b/rickandy/archive/2012/09/17/asp-net-mvc-4-mobile-caching-bug-fixed.aspx