2011年5月18日

.net如今已经很流行,成为赶时髦的程序员的首选。但是,大量刚刚接触.net的程序员的确存在一定的认识误区,这里先介绍一部分。

一、.net程序再运行一次就会更快

许多人对此的解释是:.net程序第一次运行时会被编译成本地代码,所以再次运行会更快。但遗憾的是,其实每次运行,那些IL都会被翻译一次,不会保留下来。所以并不会变快。但为什么有时候真的觉得快了呢?其实所有程序都是这样,这是Windows再为你缓存用过的组件。真正需要CPU时间的程序,多运行是不会加快速度的。

二、.net程序运行起来一定很慢

由于存在IL被翻译成本地代码的过程,.net程序的确要消耗一部分时间。但是.net程序仍然具有很高的效率,这一点许多Java虚拟机都比不上,这是为什么呢?因为多数.net程序内含的代码很少,几乎都是调用.net Framework中的类库,而这些类库在.net Framework安装的时候全部编译成为本机优化的本地码,并保存在程序集缓存里面。有了这个程序集缓存,你程序事实上的大部分已经是高效的本地代码,所以当然快了。但是,如果你进行大量数值计算类的操作,还是能够感到第一次运算的速度明显下降。所以建议大家将计算密集型的程序分离出来,编译成本地代码并保存在程序集缓存中,以后使用起来就很快了。

三、C#比VB.net快,功能更强大

必须得承认,C#编译器和VB.net编译器在处理某些细节上确实有些不同,导致两者编译功能类似的程序效率通常会有差异。但对于一个编制完整的项目,是不会有任何差别的。原因是首先VB.net编译器并没有比C#有实质上的缺陷,其次大部分代码都是.net Framework类库中的代码,两者调用起来没有差别。至于C#比VB.net功能强大,这牵扯到心理学问题。VB并不比C#差,也没有“语法混乱”、“为了兼容而设计”之类传说中的毛病。用VB.net一样可以写出出色的、结构完美的应用程序。而且VB.net也是Visual Studio.net唯一的宏语言。

这里面还有一个问题,著名的Linux下的.net——Mono中是否只支持C#?当然不是,用VB.net开发出的程序一样可以在Mono中正确运行。而且最新的Mono将包含Mono Basic语言,这样,掌握VB.net语法的人,就可以完全在Linux等系统下开发。

四、微软只想推广C#,他想放弃VB.net

不要听信杞人忧天的话。微软无时无刻不在推广VB.net上用心。MSDN上关于.net开发的文章,用VB.net做例子的比用C#还要多。微软还专门推出VB技术节目——VBTV,帮助开发者更好地了解VB.net。微软在列出Visaual Studio中的开发工具时,Visual Basic总是排在第一个——这是微软5年来的传统。所以担心VB.net会被淘汰的人,现在该松口气了。

五、微软用.net对抗Java,所以我也要支持.net,反对Java

千万不要有这种想法,MS当然想占领Java的市场,Sun当然不愿意,但这不关你的事。哪一方赢了你都不会有实质上的好处。还是两个都学吧。与其在网上挑起争论,不如多学一个本事,你马上就会看到回报的。

好了,从下一次开始,将着重介绍技术上的误区。祝大家好运

出处:http://www.webjx.com/aspnet/2010-04-12/23006.html

posted @ 2011-05-18 16:06 lovecaviar 阅读(19) 评论(0) 编辑

2011年3月16日

A Potentially Dangerous Request.Form Value was Detected From The Client

Let's say we are creating a simple form in our ASP.NET MVC 3 web application and there is a Body field on the form where we want to allow HTML Tags.

 

ASP.NET MVC 3 Form

 

If we do not disable request validation in some manner for this Body field, we will get the dreaded error - A potentially dangerous Request.Form value was detected from the client (Body = "<br>").

 

A potentially dangerous Request.Form value

 

Request validation is a good thing since it keeps people from injecting script tags in our application for Cross-Site Scripting ( XSS ) attacks. However, in this case we want to disable request validation on the Body Field so we can put HTML in the body of our blog posts.

 

ValidateInput Attribute

In ASP.NET MVC 2 we used the ValidateInput Attribute on the action to disable request validation for the entire request ( See ValidateInput Attribute in ASP.NET MVC - Potentially Dangerous Request.Form Values ).

 

ValidateInputAttribute

 

The downfall of this approach is that the ValidateInputAttribute disables request validation on all model properties, and we just want to disable request validation on a single property, called Body.

 

AllowHtmlAttribute in ASP.NET MVC 3

In ASP.NET MVC 3 we now have a property attribute that we can include on model properties to disable request validation on a property by property basis, called AllowHtmlAttribute. Instead of using the ValidateInputAttribute on the action, we turn off request validation just on Body by adding the [AllowHtml] Attribute to it:

 

AlowHtmlAttribute

 

This allows HTML for the Body Property, but does not allow HTML for the Title Property, which is what we want.

[Note: Briefly in ASP.NET MVC 3, before it was released, there existed a SkipRequestValidationAttribute. It no longer exists and has been renamed to AllowHtmlAttribute.]

 

Conclusion

Unless you want request validation disabled for the entire request, I recommend using the AllowHtmlAttribute instead of the ValidateInputAttribute when migrating your web applications from ASP.NET MVC 2 to ASP.NET MVC 3.

Learn more about new ASP.NET MVC 3 features. Check out ASP.NET MVC Books.

Hope this helps.

David Hayden

posted @ 2011-03-16 09:57 lovecaviar 阅读(480) 评论(1) 编辑