關(guān)于ASP.NET的只言片語[2008.8.12最后更新]
在學(xué)習(xí)的過程中,總是會有一些細節(jié)的問題,它很小,也很容易解決,但有時候它還真的挺有用,于是,便產(chǎn)生了寫此隨筆的念頭。這將是一篇不斷更新的隨筆,記錄一些平常碰到的ASP.NET的“小”問題。
1. 問題:一個頁面,它有一個TextBox,還有一個RequiredFieldValidator驗證控件(Text="...不能為空"),頁面中還有一個退出按鈕(服務(wù)器控件,OnClick="Logout")。在點擊“退出”按鈕時,RequiredFieldValidator驗證控件會起作用,顯示“...不能為空”,請求無法回傳,也就導(dǎo)致了不能退出。[2008.6.10]
解決辦法:設(shè)置“退出”按鈕的屬性:CausesValidation="false"
2. 為什么一旦瀏覽器不啟用Cookie,Session也不能用,不是說Session是保存在服務(wù)器么?[2008.8.6]
答:When you use Session state, a session cookie named ASP.NET_SessionId is added to your browser automatically. This cookie contains a unique identifier. It is used to track you as you move from page to page.When you add items to the Session object, the items are stored on the web server and not the web browser. The ASP.NET_SessionId cookie is used to associate the correct data with the correct user. (A session cookie exists only in memory. If a user closes the web browser, the session cookie disappears forever).
-- Stephen Walther 《ASP.NET 2.0 Unleashed》
-- Stephen Walther 《ASP.NET 2.0 Unleashed》
4. Cookieless Session的注意點[2008.8.6]:
When you enable cookieless session state, you should also enable this attribute because it can help prevent users from inadvertently sharing session state.
<sessionState cookieless="AutoDetect" regenerateExpiredSessionId="true" />
-- Stephen Walther 《ASP.NET 2.0 Unleashed》
5. 關(guān)于HttpServerUtility.Execute(string path, TextWriter writer, bool preserveForm)方法中的preserveForm參數(shù)[2008.8.6]:
如果將此參數(shù)設(shè)為true,則會把傳給本頁面的querystring和request.form中的信息傳給被Excute的那個頁面,比如本頁地址為Default.aspx?p=5,頁中有代碼:Server.Execute("Default2.aspx", sw, true),sw是StringWriter的實例,那么,頁面Default2.aspx中就可以通過Request.QueryString["p"]得到5,反之,如果把preserveForm設(shè)為false,則Default2.aspx中就取不到url參數(shù)p的值。
6. 在Visual Studio中編寫皮膚文件(*.skin)時沒有智能感知?[2008.8.10]
答:是可以設(shè)置的,在VS中Tools --> Options --> Text Editor --> File Extension, 在右側(cè)的Extension中添加skin,然后選擇User Control Editor,確定,然后關(guān)掉*.skin文件再打開就行了,圖如下:

7. 自定義異常不要從ApplicationException繼承,而要從Exception繼承:[2008.8.12]
這點放在這篇文章里或許不是太合適,但現(xiàn)在沒地兒可放,就先擱這兒了。這點用Krzysztof Cwalina和Jeffery Richter的話來解釋:
Jeffery Richter: System.ApplicationException is a class that should not be part of the .NET Framework. The original idea was that classes derived from SystemException would indicate exceptions thrown from the CLR (or system) itself, whereas non-CLR exceptions would be derived from ApplicationException. However, a lot of exception classes didn’t follow this pattern. For example, TargetInvocationException (which is thrown by the CLR) is derived from ApplicationException. So, the ApplicationException class lost all meaning. The reason to derive from this base class is to allow some code higher up the call stack to catch the base class. It was no longer possible to catch all application exceptions.
Krzysztof Cwalina: Today, somebody asked me whether they should go through their code-base and remove all references to the exception. I answered that it’s not worth the effort. “ApplicationException is not considered harmful, just useless.”
浙公網(wǎng)安備 33010602011771號