2014年6月26日 星期四

.NET MVC 筆記


MVC筆記

規劃兩個月製作MVC簡報,也告一段落了,紀錄其他延伸閱讀的筆記



ASP.NET MVC使用筆記
http://catchtest.pixnet.net/blog/post/29156132-asp.net-mvc%E4%BD%BF%E7%94%A8%E7%AD%86%E8%A8%98


格式化 Javascript
http://www.css88.com/tool/js_beautify/


ASP.NET MVC專案最佳化
http://blog.kkbruce.net/2013/07/asp-net-mvc-project-performance-security-up-tips.html#.U5_1ZvmSylA


確保傳輸資料的安全
http://ithelp.ithome.com.tw/question/10137427


使用输出缓存提高性能
http://www.360doc.com/content/11/0303/22/6101874_97895851.shtml


ASP.NET性能優化之讓瀏覽器緩存動態網頁

http://rritw.com/a/bianchengyuyan/_NET/20110915/128804.html


黑暗執行序 - ASP.NET MVC路由練習-API分版
http://blog.darkthread.net/post-2013-09-16-aspnet-mvc-versioning-via-routing.aspx



快快樂樂學LINQ系列
http://www.dotblogs.com.tw/hatelove/category/6084.aspx


如何提昇LINQ 的效能!?
http://flashlin.wordpress.com/2009/03/24/%E5%A6%82%E4%BD%95%E6%8F%90%E6%98%87linq-%E7%9A%84%E6%95%88%E8%83%BD/


ADO.Net Entity Framework : (二十) 提升EF執行速度方法
http://www.dotblogs.com.tw/asdtey/archive/2009/10/28/efimproveperformance.aspx?fid=58201




2014年6月23日 星期一

一般View與Partial View有什麼不同呢?



from : http://arthurmvc.blogspot.tw/2012/11/aspnet-mvcview.html


在上一章中,我曾提到過有關Controller的命名會受到限制,所有Controller都必需是Controller這幾個英文字作為結尾,同理可知,想必這裡應該也是在命名原則上作文章,是的!在View中若是以底線作為檔案名稱開頭的View,其意義就是這個View是一種Partial View!

    一般View與Partial View有什麼不同呢?
   我可以把它想作成:一般View是一個我們預期它為一個既完整又是最終的畫面呈現,而Partial View是畫中的某一部份。有的時候,一個View太過巨大在設計及維護上有著很高的障礙,為了避免太過複雜而讓自己陷入設計的泥淖,通常,會將一個View中的某幾個部份獨立出來成為Partial View。在本文前面所述,在專案的開發過程中,有的時候我們會發現某些頁面會有一個共同的部份,這個時候通常我們也會將其共同的部份切割出來成為一個Partial View並放置在Views/Share資料夾中共用。





2014年6月20日 星期五

[MVC] - ViewBag 裡使用方法(Method)

from: http://kevintsengtw.blogspot.tw/2012/04/aspnet-mvc-3viewbag-method.html#.U6QCbPmSylA



        //
        // GET: /ViewDataBag/
        public ActionResult func()
        {
            ViewData["func1"] = DateTime.Now;
            ViewBag.func1 = new Func<int, string>(GetStr);

            return View();
        }

        private string GetStr(int iNumber)
        {
            if (iNumber >= 60)
            {
                return iNumber.ToString() + " 及格";
            }
            else
            {
                return iNumber.ToString() + " 不及格";
            }
        }











2014年6月19日 星期四

[MVC] 停止瀏覽器快取網頁 for ASP.NET


From  http://jhshen.blogspot.tw/2010/07/for-aspnet-mvc.html



瀏覽器為了加速網頁讀取,通常會採用快取,
所以使用者登出後,按下"回上一頁"幾乎都可以看到登出前瀏覽過的資料。
要避免使用者登出後使用"回上一頁"功能,最簡單的方式就是讓頁面不被快取,
在網路上找了一些方法,都是在<head></head>裡面加上:
//將網頁設為立即過期。
<meta http-equiv="Expires" content="0" />
//舊寫法,為了增加相容性。
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="Pragma-directive" content="no-cache" />
<meta http-equiv="Cache-Directive" content="no-cache" />

不過這樣做,不知道為什麼在 IIS 6 的環境上,執行 ASP.NET MVC 的 Web Application 還是可以讓使用者按下"回上一頁"...
後來發現也可以直接加在 http response headers 上,
因此,改寫了一下專案裡面 base Controller 在 Initialize 時,設定 Response 的 cache 設定,設定如下:
protected override void Initialize(RequestContext requestContext)
{
    base.Initialize(requestContext);

    requestContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    requestContext.HttpContext.Response.Cache.SetExpires(DateTime.MinValue);
    requestContext.HttpContext.Response.Cache.SetNoStore();
}





2014年6月17日 星期二

[chrome] network 時間說明



from
http://stackoverflow.com/questions/10537399/what-does-the-times-mean-in-google-chromes-timeline-in-the-network-panel




  • DNS lookup: Time spent performing the DNS lookup. (you need to find out IP address of site.com and this takes time)
  • Blocking: Time the request spent waiting for an already established connection to become available for re-use. As was said in another answer it does not depend on your server - this is client's problem.
  • Connecting: Time it took to establish a connection, including TCP handshakes/retries, DNS lookup, and time connecting to a proxy or negotiating a secure-socket layer (SSL). Depends on network congestion.
  • Sending - Time spent sending the request. Depends on the size of sent data (which is mostly small because your request is almost always a few bytes except if you submit a big image or huge amount of text), network congestion, proximity of client to server
  • Waiting - Time spent waiting for the initial response. This is mostly the time of your server to process and respond to your response. This is how fast if your server calculating things, fetching records from database and so on.
  • Receiving - Time spent receiving the response data. Something similar to the sending, but now you are getting your data from the server (response size is mostly bigger than request). So it also depends on the size, connection quality and so on.



  • chrome官方文件

    https://developer.chrome.com/devtools/docs/network



    2014年6月15日 星期日

    [MVC] Two Html.ActionLink in a column using WebGrid

    form http://forums.asp.net/t/1653111.aspx?Two+Html+ActionLink+in+a+column+using+Webgrid



    columns: grid.Columns(
    
    
    grid.Column("id"),
    grid.Column("Name"),
    grid.Column(header: "Actions", format: (item) =>
         new HtmlString(
               Html.ActionLink("Edit", "Edit", new { id = item.id } ).ToString()  +
               Html.ActionLink("Delete", "Delete", new { id = item.id } ) .ToString()
         )
    )
    )







    2014年6月14日 星期六

    [C#] Entity與DataTable互轉

    from http://www.dotblogs.com.tw/hatelove/archive/2011/12/01/entity-datatable-converter-using-reflection.aspx





            /// <summary>
            /// 將entities直接轉成DataTable
            /// </summary>
            /// <typeparam name="T">Entity type</typeparam>
            /// <param name="entities">entity集合</param>
            /// <returns>將Entity的值轉為DataTable</returns>
            public DataTable EntityToDataTable<T>(IEnumerable<T> entities)
            {
                var result = GetEntityToDataTableSchema(typeof(T));
                if (entities.Count() == 0)
                {
                    return result;
                }

                var properties = typeof(T).GetProperties();

                foreach (var entity in entities)
                {
                    var dr = result.NewRow();

                    foreach (var property in properties)
                    {
                        dr[property.Name] = NullToDbNull(property.GetValue(entity, null));
                    }

                    result.Rows.Add(dr);
                }

                return result;
            }


            /// <summary>
            /// Gets the entity to data table schema.
            /// </summary>
            /// <param name="entityType">Type of the entity.</param>
            /// <returns>對應Entity屬性型別的DataTable</returns>
            public DataTable GetEntityToDataTableSchema(Type entityType)
            {
                var result = new DataTable();

                var properties = entityType.GetProperties();

                foreach (var property in properties)
                {
                    var columnType = property.PropertyType;

                    if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                    {
                        //兩種寫法都可以,透過Nullable.GetUnderlyingType()比較簡潔。

                        //columnType = property.PropertyType.GetGenericArguments()[0];
                        columnType = Nullable.GetUnderlyingType(property.PropertyType);
                    }

                    var column = new DataColumn(property.Name, columnType);
                    result.Columns.Add(column);
                }

                return result;
            }

            ///// <summary>
            ///// 若值為DBNull.Value, 則轉為Null
            ///// </summary>
            ///// <param name="original"></param>
            ///// <returns></returns>
            //public object DbNullToNull(object original)
            //{
            //    return original == DBNull.Value ? null : original;
            //}


            /// <summary>
            /// 若值為null, 則轉成DBNull.Value
            /// </summary>
            /// <param name="original"></param>
            /// <returns></returns>
            public object NullToDbNull(object original)
            {
                return original ?? DBNull.Value;
            }






    2014年6月13日 星期五

    [C#] 關鍵字 async、await

    from http://www.dotblogs.com.tw/simplecestlavie/archive/2012/06/26/73080.aspx




    async  (非同步)
    代表該 Method 中可能會用到 await (也就是讓 Compiler 知道有 await,並且在這個地方下斷點)

    await  (同步)
    代表這個 Method 為 awaitable 的 Method,也就是說,Compiler 會在這個地方下斷點。




    2014年6月7日 星期六

    [MVC] WebGrid 欄位用法

    from:  http://stackoverflow.com/questions/4306683/how-to-make-a-mvc-3-webgrid-with-checkbox-column





      @{
        View.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
    
        var usersGrid = new WebGrid(source: Model,
            rowsPerPage: 40);
    }
    @usersGrid.GetHtml(
            tableStyle: "grid",
            headerStyle: "head",
            alternatingRowStyle: "alt",
                    columns: usersGrid.Columns(
                        usersGrid.Column(format: (item) => 
                             Html.ActionLink("Edit", "Edit", new { id = item.Id})),
                        usersGrid.Column("Surname")
            )
        )

    usersGrid.Column(format: (item) => Html.CheckBox("Id"))







     

    MangoHost Copyright © 2009 Cookiez is Designed by Ipietoon for Free Blogger Template