c# - 大量自定义路由约束会减慢站点速度?

标签 c# asp.net-mvc performance redirect asp.net-mvc-routing

它不会一直发生,但在大量使用后,我的网站似乎变慢了很多,我唯一更改的代码是更改重定向。

路由查找旧类别(不再使用)并将其替换为新类别。所以有人如何去 example.com/scary-shows/buffy 将被重定向到 example.com/television/buffy

我正在尝试找出性能损失(如果它甚至发生在这里),以及我可以改进它的地方。

全局.asax:

...
// custom constraint
    routes.MapRoute("ArticlesCategories", "{category}/{page}",
       new { controller = "Custom", action = "CategoryRedirect" },
       new { isCategory = new CategoryRouteConstraint() });

    //Article routes            
    routes.MapRoute("ArticlesRedirect", "Articles/Redirect",
       new { controller = "_Articles", action = "Redirect" });
             routes.MapRoute("ArticlesSubcategories", "articles/categories/{filter}/{section}",
                    new { controller = "_Articles", action = "Filter", filterBy = "categories", section = UrlParameter.Optional });
          routes.MapRoute("ArticlesTags", "articles/tags/{filter}",
    new { controller = "_Articles", action = "Filter", filterBy = "tags" });
...

约束:

   public class CategoryRouteConstraint:IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values,
                          RouteDirection routeDirection)
        {
            string page = string.Format("{0}", values["page"]);
            string urlCategory = string.Format("{0}", values["category"]);

            if (GetCategoryDictionary().ContainsKey(urlCategory))
            {
                string topCategory = GetCategoryDictionary()[urlCategory];
                values.Add("topCategory",topCategory);
                return true;
            }
            return false;
        }


        private static List<Category> _categoryList = null;
        private static object _lockObj = new object();
        private static Dictionary<string, string> _dictionary = null;

        private static Dictionary<string, string> GetCategoryDictionary()
        {
            if (_dictionary != null) return _dictionary;

            lock (_lockObj)
            {
                //check again...
                if (_dictionary != null) return _dictionary;
                var dictionary = new Dictionary<string, string>
                    {
                        {"my-old-example-category", "example"},
                        {"blogs", "Blog"},
                        {"doctor-notes", "Blog"},
                        {"action-movies", "movies"},
                        {"reality-tv", "television"},
                        {"scary-shows", "televsion"},
                        ... this goes on for about 100 more categories ...
                    };

                _dictionary = dictionary;
                return dictionary;
            }
        }
    }

Controller :

[OutputCache(Duration = 10800)]
public class CustomController : Controller
{
    public ActionResult CategoryRedirect()
    {
        string topCategory = string.Format("{0}", RouteData.Values["topCategory"]);
        string page = string.Format("{0}",RouteData.Values["page"]);

        return RedirectPermanent(string.Format("/{0}/{1}", topCategory, page));
    }
}

最佳答案

您可以稍微优化一下您的代码:

  • 使用 .ToString() 而不是 string.Format(string.format 使用昂贵的正则表达式)
  • 使用 TryGetValue() 而不是您的 2 个操作
  • 在应用程序启动时初始化字典,甚至考虑完全删除字典,带有返回字符串的大型 switch 语句比字典更快

关于c# - 大量自定义路由约束会减慢站点速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17882761/

相关文章:

c# - 登录loginform并在form 2中看到

c# - 从 JQuery 将 JSON 发布到 ASP.NET MVC 4 操作

asp.net-mvc - ASP.NET MVC 真的是 MVC 吗?或者如何将模型与 Controller 分离?

C++程序在管道时表现更好

c# - 从不同的表中检索数据

c# - 如何从 html 输入按钮单击调用 Windows Phone 8 代码隐藏方法

c++11正则表达式比python慢

iphone - 使用矢量化为 iPhone 编译 Eigen 库

c# - 带有 Dbcontext 的 ExecuteStoreQuery

asp.net-mvc - _Layout 的 ASP.Net MVC Controller