匿名类和 IDictionary速度差异对于 ASP.NET MVC 中的 htmlAttributes

发布于 2024-08-22 03:03:26 字数 913 浏览 3 评论 0原文

我正在尝试使用一些技术来优化我的 ASP.NET MVC 应用程序,其中包括 URL 生成调整: http://www.chadmoran.com/blog/2009/4/23/optimizing-url- Generation-in-aspnet-mvc- part-2.html

如果使用 RouteValueDictionary 代替匿名类之间的速度差异如此之大,那么在定义 html 属性时我是否也应该考虑使用 Dictionary 代替匿名类?

例如,我应该这样做:

Html.ActionLink("LinkName", "Action", "Controller",
                new RouteValueDictionary { { "id", Model.Id } },
                new { @class = "someCSSClass" })

还是应该通过这样做进一步优化:

Html.ActionLink("LinkName", "Action", "Controller",
                new RouteValueDictionary { { "id", Model.Id } },
                new Dictionary<string, object> { { "class", "someCSSClass" } })

我知道使用 Url.Action 更快,或者使用 RouteLink 技术更好,但我只是想知道是否应该完全避免匿名类为了速度。

I am trying to optimize my ASP.NET MVC application using some techniques that include URL generation tweaking a la: http://www.chadmoran.com/blog/2009/4/23/optimizing-url-generation-in-aspnet-mvc-part-2.html

If the speed difference is so large between using RouteValueDictionary in place of anonymous classes, then should I also look to use Dictionary in place of anonymous classes when defining html attributes?

For example, should I do this:

Html.ActionLink("LinkName", "Action", "Controller",
                new RouteValueDictionary { { "id", Model.Id } },
                new { @class = "someCSSClass" })

or should I further optimize by doing this:

Html.ActionLink("LinkName", "Action", "Controller",
                new RouteValueDictionary { { "id", Model.Id } },
                new Dictionary<string, object> { { "class", "someCSSClass" } })

I know it's even faster to use Url.Action, or better yet to use the RouteLink technique, but I'm just wondering whether anonymous classes should be completely avoided for the sake of speed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

め七分饶幸 2024-08-29 03:03:26

是的,使用词典更快。

它的速度是否足以带来改变?对于您的应用程序,只有分析器才能告诉您这一点。我建议,如果它确实产生了影响,那么您无论如何都应该缓存您的视图结果。

不过,我倾向于坚持使用字典版本,因为强类型有助于消除 ActionLink 的疯狂重载。传递对象很容易导致错误的重载。速度只是一个奖励。

Yes, it's faster to use the Dictionary.

Is it fast enough to make a difference? Only a profiler can tell you that, for your application. I'd suggest that if it actually makes a difference then you should be caching your view results anyway.

I tend to stick with the Dictionary versions, though, because the strong typing helps to cut through the insane mess of overloads to ActionLink. Passing an object makes it way too easy to end up with the wrong overload. The speed is just a bonus.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文