ASP.NET MVC3 - TempData的Session消失問題

網友一篇「 MVC 3上,Session消失的問題與解決辦法」引起我的興趣,我留言要了些基本的Code,以下是我驗證程式碼,結論是,Session並不會消失,怪怪!

Step 1. 新增Controller及Action
以下程式碼為驗證使用,沒有進行任何安全過濾,請不要使用。

Imports System.IO 

Namespace UpDownFileFromDBMvc
    Public Class FileUploadAndSessionTestController
        Inherits System.Web.Mvc.Controller

        '
        ' GET: /FileUploadAndSessionTest/Upload

        Function Upload()
            Return View()
        End Function

        
        Function Upload(upfile As HttpPostedFileBase )
            If upfile  IsNot Nothing andalso upfile.ContentLength > 0 Then
                Dim savePath As String = Path.Combine(Server.MapPath("~/Files/"), upfile.FileName)
                upfile.SaveAs(savePath)
                TempData("FileName") = upfile.FileName 
            End If

            Return RedirectToAction("FileInfo")
        End Function

        Function FileInfo()
            ViewBag.FileName = TempData("FileName")
            Return View()
        End Function

    End Class
End Namespace

以上參考該網友提供程式碼,我所撰寫的VB程式碼。

Step 2. 新增Upload與FileInfo的View

Upload.aspx

<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Upload
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Upload</h2>
<%Using Html.BeginForm("Upload", "FileUploadAndSessionTest", FormMethod.Post, New With {.enctype = "multipart/form-data"})%>
    <%: Html.ValidationSummary(True)%>
    File:<input id="upfile" name="upfile" type="file" value="" />
    <br />
    <input type="submit" value="Upload" />
<%End Using%>
</asp:Content>

FileInfo.aspx

<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
FileInfo
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>FileInfo</h2>
Upload File Nale: <%: ViewBag.FileName %>
</asp:Content>

以下為執行結果圖。

Upload File then Pass file name
圖一:Upload File then Pass file name(點擊看大圖)
程式唯一不同處只有路徑我有先使用Path.Combine處理過,不知道是不是這個原因。

重構 - List of Sound Bites(原音重現)

以下節錄《重構-改善即有的設計》一書最後一篇的重點清單,此章我認為是《重構-改善即有的設計》此書中最重要的一章,它提出讓程式裡不應該有的壞味道。
p.7
When you find you have to add a feature to a program, and the program’s code is no structured in a convenient way to add the feature, first refactor the program to make it easy to add the feature, then add the feature.
如果你發現自己需要為或式添加一個特性,而程式碼結構使你無法很方便地那麼做,那就先重構那個程式,使特性的添加比較容易進行,然後再添加特性。
p.8
Before you start refactoring, check that you have a solid suite of tests. These must be self-checking.
重構前,先檢查自己是否有一套可靠的測試機制。這些測試必須有自我檢驗能力。
p.13
Refactoring changes the programs in small steps. If you make a mistake, it is easy to find the bug.
重構技術係以微小的步伐修改程式。如果你犯下錯誤,很容易便可發現它。
p.15
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
任何一個傻瓜都能寫出電腦可以理解的程式碼。唯有寫出人類容易理解的程式碼,才是優秀的程式員。
p.53
Refactoring (noun): a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior of the software.
重構 (名詞):對軟體內部結樣的一種調整,目的是在不改變「軟體之可察行為」前提下,提高其可理解性,降低其修改成本。
p.54
Refactor (verb): to restructure software by applying a series of refactorings without changing the observable behavior of the software.
重構 (重詞):使用一系列重構準則(手法),在不改變「軟體之可察行為」前提下,調整其結構。
p.58
Three strikes and you refactor.
事不過三,三則重構。
p.65
Don’t publish interfaces prematurely. Modify your code ownership policies to smooth refactoring.
不要過早發佈介面。請修改你的程式碼擁有權政策,使重構更順暢。
p.88
When you feel the need to write a comment, first try to refactor the code so that any comment becomes superfluous.
當你感覺需要撰寫註釋,請先嘗試重構,試著讓所有註釋都變得多餘。
p.90
Make sure all tests are fully automatic and that they check their own results.
確保所有測試都完全自動化,讓它們檢查自己的測試結果。
p.90
A suite of tests is a powerful bug detector that decapitates the time it takes to find bugs.
一整組測試就是一個強大的臭蟲偵測器,能夠大大縮減搜尋臭蟲所需的時間。
p.94
Run your tests frequently. Localize tests whenever you compile – every test at least every day.
頻繁地執行測試。每次編譯請把測試也考慮進去 – 每天至少執行每個測試一次。
p.97
When you get a bug report, start by writing a unit test that exposes the bug.
每檔你接獲臭蟲提報,請先撰寫一個單元測試來揭發這隻臭蟲。
p.98
It is better to write and run incomplete tests than not to run complete tests.
編寫未臻完善的測試並實際執行,好過對完美測試的無盡等待。
p.99
Think of the boundary conditions under which things might go wrong and concentrate your tests there.
考慮可能出錯的邊界條件,把測試火力集中在那兒。
p.100
Don’t forget to test the exceptions are raised when things are expected to go wrong.
當事情被大家認為應該會出錯時,別忘了檢查彼時是否有異常被如期拋出。
p.101
Don’t let the fear that testing can’t catch all bugs stop you from writing the tests that will catch most bugs.
不要因為「測試無法捕捉所有臭蟲」,就不撰寫測試碼,因為測試的確可以捕捉到大多數臭蟲。
我個人是對p.88這一條有些意見。但事後想想,他的意思應該是讓程式自我解釋,不過這有很大的難度。相反的,在一些教科書或實作上,我們希望註解是越清楚越好。

小孩帶金剛 - 什麼是寵愛與溺愛?

上週,小女因病住院一週。幾次住院的日子裡,可以碰到各式各樣的照顧者,父母、祖父母、舅舅、姑媽…其中以父母與祖父母最多。

在院照顧的日子多了,碰過許多讓人…搖頭的照顧者,例如,有一次碰到一位國中小女生與祖母,這位小女生把病房當自己家,帶著一個Radio來病房聽音樂,這沒什麼關係,不過晚上九點…十點…十一點…她們還在聽,而且還在和祖母聊天。越晚越靜時,一點點小聲音就顯的很大聲,我們可以忍受(或說習慣)孩子不舒服所產生聲音,但音樂加聊天聲,實在讓人無法休息。更好笑的話,隔天下午約兩、三點時,這對祖孫可能是前一天聊太晚累了,想要休息,這位小女生還用不小的音量說:「聊天的聲音好吵哦,我都沒辦法睡午覺。」

重點是這位長輩,從頭到尾都是和孫女同一陣線,讓人直搖頭。

不過,這次住院碰到Particular Case,讓我深刻了解寵愛與溺愛,算是另類「小孩帶金剛」的教育。

Tool - Longman Dictionary of Contemporary English, 5th Edition In Windows 7 No Voice

如果你的Longman Dictionary of Contemporary English, 5th Edition (2009) 在Windows 7中無法播放聲音,試試以下解決方案。

Step by Step 1:控制台 → 聲音 → 喇叭 → 右鍵 → 設定喇叭 → 立體聲

如果你的硬體規格是比較新的,可能音效支援可能有「立體聲」、「四聲道」、「5.1環場音效」、「7.1環場音效」,Longman Dictionary of Contemporary English, 5th Edition (2009)只能在立體聲下播放。

Step by Step 2:控制台 → 聲音 → 喇叭 → 右鍵 → 內容 → 等級 → CD音訊 → 不能靜音

Windows 7 CD音訊設定
圖一:CD音訊設定

金剛帶小孩 - 是誰殺了孩子的想像力?

當了爸爸後,會很注意孩子的變化,這些變化有好有壞,有些可避免,有些無法避免。

有孩子的家裡,通常玩具一堆,大玩具、小玩具、滿地整床的玩具,不過在我們家我買的玩具不多,通常是玩到壞了,玩到支離破碎。大寶貝,媽媽教的好,玩具通常是老二玩壞比較多,原因也簡單,因為她才一歲多,正是破壞力超強的年紀,給她破壞王的稱呼絕不過分。

最近,買了一個電話玩具,之前的電話玩具玩壞了,每次看著大寶、二寶拿著電話在對空氣聊天,我都覺的好可愛,現在的玩具都聲光效果很好,按1會說One,按2會說Two,按有馬圖案按鍵會有馬叫聲然後說horse,不然就是Do,Ri,Me,唱歌根本小事。