简体   繁体   English

C#MVC:MVC Html助手与视图中的直接HTML的性能和优势

[英]C# MVC: Performance and Advantages of MVC Html Helpers vs. Direct HTML in views

I'd like to know what kind of performance impact Html helpers have on C# ASP.NET MVC views, especially when setting attribute parameters, and what kind of advantages they have overall (why use them?) 我想知道Html助手对C#ASP.NET MVC视图有什么样的性能影响,特别是在设置属性参数时,以及它们总体上有哪些优点(为什么要使用它们?)

With Html Helpers: 使用Html助手:

<%= Html.TextBox("firstName", Model.FirstName, 
    new { @disabled = "disabled", @class = "myCssClass" }) %>

Direct Html: 直接Html:

<input type="text" class="myCssClass" name="firstName" 
     disabled="disabled" text="<%= Model.FirstName %>"/>

I have quite a few pages that contain between 5 and 15 of such inputs. 我有很多页面包含5到15个这样的输入。 On top of that Html Helpers allow you to render the form (think Html.BeginForm()) etc. so you potentially end up with 20 or even more Html Helper calls. 最重要的是,Html Helpers允许你渲染表单(想想Html.BeginForm())等,这样你最终可能会得到20个甚至更多的Html Helper调用。 I think some of them use reflection too, eg when you set attributes like the disabled one above. 我认为其中一些也使用反射,例如当您设置上面的禁用属性时。

Isn't there a huge performance impact to do this? 这样做不会对性能产生巨大影响吗? Why on earth is it considered better practice to use those helpers? 为什么使用这些助手被认为是更好的做法? Please somebody give me a good reason :) I'd like to use them but I really fear the performance impact they have. 请有人给我一个很好的理由:)我想使用它们,但我真的担心它们对性能的影响。

Are there any real advantages to using Html helpers? 使用Html助手有什么真正的好处吗?

The overhead of doing reflection is something that people really like to worry about. 做反思的开销是人们真正想要担心的事情。 Outside of synthetic benchmarks, however, it becomes a pretty boring topic! 然而,在综合基准测试之外,它变得非常无聊!

In the context of a real production application (where you are doing CRUD operations against a databases or consuming a webservice for example), the overhead of using html helpers is going to be insignificant compared to the overhead of doing that kind of context switch. 在实际生产应用程序的上下文中(例如,您正在对数据库执行CRUD操作或使用Web服务),与执行此类上下文切换的开销相比,使用html帮助程序的开销将是微不足道的。

Really not something to worry about especially considering the benefits html helpers provide such as automatically restoring form values from ViewData/Model, and validation support. 真的不用担心,尤其是考虑到html帮助程序提供的好处,例如从ViewData / Model自动恢复表单值以及验证支持。

Bottom line: use html helpers when possible. 底线:尽可能使用html助手。 You can always use straight html if you encounter a rare limitation that you need to work around. 如果您遇到需要解决的罕见限制,您可以随时使用直接HTML。

Are there any real advantages to using Html helpers? 使用Html助手有什么真正的好处吗?

The biggest advantage I see in using HtmlHelpers is to provide an abstraction layer for your markup. 我在使用HtmlHelpers时看到的最大优势是为您的标记提供了一个抽象层。 If in the future you wanted to change the structure of your markup you only need to change the output generated from the helpers, as opposed to going through all your views and making manual changes. 如果将来您想要更改标记的结构,则只需更改帮助程序生成的输出,而不是浏览所有视图并进行手动更改。

It also promotes consistency amongst teams of developers. 它还促进了开发人员团队之间的一致性。 Those developers aren't required to know the exact details of the markup structure and css classes your UI is based on. 这些开发人员不需要知道标记结构和UI所基于的CSS类的确切细节。

As an example, I'm currently developing a new UI framework for the company I work for based on Bootstrap. 作为一个例子,我正在为我为基于Bootstrap工作的公司开发一个新的UI框架。 I have created a set of HtmlHelpers that generate the appropriate markup and css classes for the various Bootstrap components. 我创建了一组HtmlHelper,为各种Bootstrap组件生成适当的标记和css类。 This is all done in a Fluent API which is nice for developers to use, without any in-depth knowledge required of Bootstrap, plus with the added benefit of having Intellisense available. 这一切都是在Fluent API中完成的,这对开发人员来说很好用,没有Bootstrap所需的任何深入知识,还有可以使用Intellisense的额外好处。

Telerik's Kendo UI framework is based on the same concept. Telerik的Kendo UI框架基于相同的概念。 Take a look at some of their code samples. 看看他们的一些代码示例。

As for reflection and performance, I really wouldn't worry considering the number of calls likely to be involved in a few HtmlHelper methods. 至于反射和性能,考虑到可能涉及一些HtmlHelper方法的调用次数,我真的不会担心。 See this post for reasons why. 请参阅此帖子了解原因。

I've done it both ways, and the performance seems to be about the same. 我已经两种方式完成了,性能似乎差不多。 Phil Haack says you can do it either way -- that the helpers are just that, helpers, and if you prefer you can write Plain Old HTML. Phil Haack说你可以做任何一种方式 - 帮助者就是那样,帮助者,如果你愿意,你可以编写Plain Old HTML。

I'm not sure that the helpers are any safer...either way you wind up with the same HTML in the web page...but the intellisense does seem to work better in the helpers for some reason, which is nice. 我不确定帮助者是否更安全...无论你在网页中使用相同的HTML结束的方式......但是由于某种原因,intellisense似乎在助手中效果更好,这很好。

Dropdowns are easier to make with the helpers, since you don't have to spin up a loop for the selection list. 使用帮助程序更容易进行下拉菜单,因为您不必为选择列表启动循环。 Hidden fields and text boxes (and links as well) look better to my eye done in plain HTML, especially if they contain several attributes, as you are able to avoid that object initialization syntax. 隐藏字段和文本框(以及链接)在纯HTML中看起来更好,特别是如果它们包含多个属性,因为您可以避免该对象初始化语法。

Plain HTML appears to mesh well with jQuery ( see here for an example ). 纯HTML似乎与jQuery很好地融合( 参见此处的示例 )。 And the plain HTML is just easier to read. 简单的HTML更容易阅读。

I imagine helper methods being useful when you want to inject a larger structure into the html. 我想,当你想在html中注入一个更大的结构时,辅助方法很有用。 In a few months you will find websites rich with these methods, that will support all kinds of functionality. 在几个月内,您会发现富含这些方法的网站,它们将支持各种功能。 Imagine being able to inject a graph into your page with one line of code. 想象一下,能够使用一行代码将图形注入到页面中。

There isn't any reflection with HtmlHelpers. HtmlHelpers没有任何反映。 The last parameter isn't an object, its a dictionary, so lookup of values is via hashtable, not reflection. 最后一个参数不是一个对象,它是一个字典,因此值的查找是通过哈希表,而不是反射。 The HtmlHelper is nice in that its methods are safe and secure...input is encoded where necessary, security checks are peformed where needed, etc. There is a lot more to HtmlHelpers than just html rendering. HtmlHelper很好用,它的方法是安全可靠的......输入在必要时进行编码,在需要的地方进行安全检查等等.HtmlHelpers还有很多东西,而不仅仅是html渲染。

Your controller action (invoked by GET) results in many cases must be cached to achieve good performance. 您的控制器操作(由GET调用)导致必须缓存许多情况才能获得良好的性能。 So you should not worry about your View's execution time. 因此,您不必担心View的执行时间。 For me it is hard to imagine a situation where your HtmlHelper's execution time may become the performance bottleneck. 对我来说很难想象你的HtmlHelper的执行时间可能成为性能瓶颈的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM