自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET MVC使用心得:理解含義和用法

開發(fā) 后端
要想學習asp.net mvc,我個人覺的最重要的一步是知道m(xù)vc路由機制。本文總結(jié)了一些ASP.NET MVC的使用心得。

    這些天開始學習asp.net mvc,用傳統(tǒng)的asp.net已經(jīng)快四的年了,剛開始接觸asp.net mvc確認感覺有點不適應(yīng),主要體現(xiàn)在asp.net mvc的實現(xiàn)上。

ASP.NET MVC使用心得:問題總結(jié)

    問題一:要想學習asp.net mvc,我個人覺的最重要的一步是知道m(xù)vc路由機制,傳統(tǒng)的asp.net程序要想訪問一個頁面,都是根據(jù)頁面路徑來訪問,但MVC并不能直接訪問aspx頁面。

    問題二:理解MVC三部分的含義和用法。當我們創(chuàng)建一個asp.net mvc應(yīng)用程序時,系統(tǒng)會默認生成三個文件夾:
               1:Controllers,對應(yīng)MVC中的C,主要是處理所有請求與做出對應(yīng)的響應(yīng); 
               2:Models,對應(yīng)MVC中的M,相當時我們平時創(chuàng)建工程中的實體工程,只不過在MVC中它充當了存放數(shù)據(jù)模型的作用;
               3:Views,對應(yīng)MVC中的V,這里就是存放用戶訪問的頁面文件,但是這個文件不能在瀏覽器中根據(jù)路徑訪問。
    對于系統(tǒng)生成的asp.net mvc項目,我對其做了如下擴展:

    擴展點一:系統(tǒng)之所以在web工程中直接創(chuàng)建了三個文件夾,是為了更加直觀的體現(xiàn)MVC模式,真正項目中我們需要把它們分開。

    擴展點二:MVC中重要的路由處理,默認情況是在Global.asax文件中,我們也可以把這塊內(nèi)容獨立出來。

    擴展點三:把Controller類和業(yè)務(wù)邏輯分離,這里可以采用Repository模式。

ASP.NET MVC使用心得:案例DEMO

創(chuàng)建一個簡單的留言簿的項目,數(shù)據(jù)存儲采用sql,本想用linq to entity,但總覺的這部分還相關(guān)不完善,且性能存在問題,故使用傳統(tǒng)ado.net實現(xiàn)數(shù)據(jù)存儲。下面是這個項目的分層。

    1:GuestBook.Web,頁面表示層  ,MVC中的V。

    2:GuestBook.MVC.Controller,存放項目所有的Controller,MVC中的C。我們知道Controller有兩個作用:第一,處理請求;第二,做出對應(yīng)的響應(yīng)。第二點就是我們平時理解的后臺功能實現(xiàn),例如數(shù)據(jù)的增刪改查等。我們可以把這部分功能與Controller分離,即所有的業(yè)務(wù)邏輯都寫在業(yè)務(wù)邏輯層,不直接依賴Controller,我們可以進一步把這些功能點抽象出來,讓Controller依賴一個公共的接口。這個思想我之前的一篇文章有點異曲同工之處:對增刪改查用面向?qū)ο筮M行包裝

      首先:創(chuàng)建一個Repository接口:IRepository.cs,里面包含些常見數(shù)據(jù)處理操作方法:這個接口是一個泛型接口,以實現(xiàn)所有實體類的通用性。

  1. public interface IRepository< T>  
  2.     {  
  3.         List< T> FindAllInfo();  
  4.         T GetInfo(T model);  
  5.         bool  Add(T model);  
  6.         bool  Delete(T model);  
  7.         bool  Edit(T model);  
  8.     } 

     然后:實現(xiàn)一條留言的數(shù)據(jù)處理:

  1. public List< GuestBookInfo> FindAllInfo()  
  2.         {  
  3.             string sql = "select * from GuestBook";  
  4.              
  5.             List< GuestBookInfo> list = new List< GuestBookInfo>();  
  6.             using(SqlDataReader dr=SqlHelper .ExecuteReader (conn ,CommandType .Text ,sql ))  
  7.             {  
  8.                 while  (dr.Read())  
  9.                 {  
  10.                     GuestBookInfo model = new GuestBookInfo();  
  11.                     model.ID = int.Parse (dr["ID"].ToString());  
  12.                     model.sTitle = dr["sTitle"].ToString();  
  13.                     model.sContent = dr["sContent"].ToString();  
  14.                     list.Add(model);  
  15.                 }  
  16.  
  17.             }  
  18.             return list  ;  
  19.         }  
  20.         public GuestBookInfo GetInfo(GuestBookInfo model)  
  21.         {  
  22.             string sql = "select * from GuestBook where ID="+model.ID .ToString ();  
  23.             using (SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.Text, sql))  
  24.             {  
  25.                 if (dr.Read())  
  26.                 {  
  27.                     model.ID = int.Parse(dr["ID"].ToString());  
  28.                     model.sTitle = dr["sTitle"].ToString();  
  29.                     model.sContent = dr["sContent"].ToString();  
  30.                       
  31.                 }  
  32.  
  33.             }  
  34.             return model ;  
  35.         }  
  36.         public bool Add(GuestBookInfo model)  
  37.         {  
  38.             string sql = "insert into GuestBook (sTitle,sContent) values ('" + model.sTitle + "','" + model.sContent + "')";  
  39.             int i = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql);  
  40.             if (i > 0)  
  41.             { return true; }  
  42.             return false ;  
  43.         }  
  44.         public bool Delete(GuestBookInfo model)  
  45.         {  
  46.             string sql = "delete GuestBook where ID=" + model.ID.ToString();  
  47.             int i = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql);  
  48.             if (i > 0)  
  49.             { return true; }  
  50.             return false;  
  51.         }  
  52.         public bool Edit(GuestBookInfo model)  
  53.         {  
  54.             string sql = "update GuestBook set sTitle='" + model.sTitle + "',sContent='" + model.sContent + "' where ID=" + model.ID.ToString();  
  55.             int i = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql);  
  56.             if (i > 0)  
  57.             { return true; }  
  58.             return false;  
  59.         }  

      其實:Controller依賴IRepository接口。

  1. public class GuestBookController : System.Web.Mvc.Controller  
  2.     {  
  3.         IRepository< GuestBookInfo> inter = new BLL_GuestBook();  
  4.         public ActionResult Index()  
  5.         {  
  6.             var models = inter.FindAllInfo();  
  7.             return View("Index", models);  
  8.         }  
  9.        [AcceptVerbs(HttpVerbs.Post)]  
  10.        public ActionResult Create(GuestBookInfo model)  
  11.        {  
  12.              
  13.            inter.Add(model );  
  14.            return RedirectToAction("Index");  
  15.        }     
  16.        public ActionResult Create()  
  17.        {  
  18.            GuestBookInfo model = new GuestBookInfo();          
  19.            return View(model );  
  20.        }  
  21.        public ActionResult Details(int id)  
  22.        {  
  23.              
  24.            GuestBookInfo model=new GuestBookInfo ();  
  25.            model .ID =id;  
  26.            model =inter.GetInfo (model );  
  27.            if (string .IsNullOrEmpty (model.sTitle ))  
  28.            { return View("NotFound"); }  
  29.            else 
  30.            {  
  31.                return View("Details",model );  
  32.            }  
  33.        }  
  34.        public ActionResult Edit(int id)  
  35.        {  
  36.            GuestBookInfo model = new GuestBookInfo();  
  37.            model.ID = id;  
  38.            model = inter.GetInfo(model);  
  39.            if (string.IsNullOrEmpty(model.sTitle))  
  40.            { return View("NotFound"); }  
  41.            else 
  42.            {  
  43.                return View("Edit", model);  
  44.            }  
  45.        }  
  46.        [AcceptVerbs(HttpVerbs.Post)]  
  47.        public ActionResult Edit(int id, FormCollection formValues)  
  48.        {  
  49.            GuestBookInfo model = new GuestBookInfo();  
  50.            model.ID = id;  
  51.            model = inter.GetInfo(model);  
  52.            UpdateModel(model );  
  53.            inter.Edit(model);  
  54.            return RedirectToAction("Index");  
  55.        }  
  56.        public ActionResult Delete(int id)  
  57.        {  
  58.            GuestBookInfo model = new GuestBookInfo();  
  59.            model.ID = id;  
  60.            model = inter.GetInfo(model);  
  61.            if (model == null)  
  62.                return View("NotFound");  
  63.            inter.Delete(model);  
  64.            return RedirectToAction("Index");  
  65.        }  
  66.  
  67.     }  

    3:GuestBook.Model,MVC中的M。

    4:GuestBook.RouteManager,路由管理項目,把路由處理從Global.asax中分離開。我們創(chuàng)建一個新類:MyMvcAppliation.cs

  1. public  class MyMvcAppliation:HttpApplication   
  2.     {  
  3.         public static void RegisterRoutes(RouteCollection routes)  
  4.         {  
  5.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  6.  
  7.             routes.MapRoute(  
  8.                 "Default",                                              // Route name  
  9.                 "{controller}/{action}/{id}",                           // URL with parameters  
  10.                 new { controller = "Home", action = "Index", id = "" },  // Parameter defaults  
  11.                 new string[] { "GuestBook.MVC.Controller" }  
  12.             );  
  13.              
  14.  
  15.         }  
  16.  
  17.         protected void Application_Start()  
  18.         {  
  19.             ControllerBuilder.Current.DefaultNamespaces.Add("GuestBook.MVC.Controller");  
  20.             RegisterRoutes(RouteTable.Routes);  
  21.         }  
  22.     }  

    5:GuestBook.Data,數(shù)據(jù)處理工具類,例如SqlHelp等等。

    6:GuestBook.DAL,數(shù)據(jù)處理層。

    7:GuestBook.BLL,業(yè)務(wù)邏輯層。

    8:GuestBook.MyInterface,相關(guān)接口,本項目中包含Repository模式中的接口類。

     這篇文章主要是探討了MVC項目的分層以及部分擴展,歡迎大家提出更好的想法。這些就是我ASP.NET MVC的使用心得。

【編輯推薦】

  1. ASP.NET中的session存儲模式運用
  2. ASP.NET中的文件上傳下載方法集合
  3. ASP.NET中的cookie讀寫方法介紹
  4. ASP.NET中的javascript操作
  5. ASP.NET2.0中的單點登錄簡介及實現(xiàn)
責任編輯:yangsai 來源: 博客園
相關(guān)推薦

2009-07-23 15:44:39

ASP.NET MVC

2009-03-12 10:42:38

RoutingIgnoreRouteASP.NET

2009-04-20 09:43:37

ASP.NET MVC基礎(chǔ)開發(fā)

2009-07-31 12:43:59

ASP.NET MVC

2009-07-28 13:06:45

ASP.NET MVC

2009-07-24 13:20:44

MVC框架ASP.NET

2009-07-22 09:36:54

使用UpdataModASP.NET MVC

2009-06-12 09:24:34

ASP.NET窗體ASP.NET MVC

2009-07-22 16:34:36

使用T4ASP.NET MVC

2009-07-22 10:09:59

ASP.NET MVC

2009-07-23 14:31:20

ASP.NET MVC

2009-07-22 13:24:24

ASP.NET MVC

2009-07-20 10:53:59

ASP.NET MVC

2009-10-29 09:15:32

ASP.NET MVCDropDownLis

2009-07-29 09:17:12

jQuery刪除

2009-07-23 11:33:18

2009-07-22 10:13:31

異步ActionASP.NET MVC

2009-07-22 09:11:02

Action方法ASP.NET MVC

2009-04-01 12:00:43

ASP.NETMVC

2009-07-20 15:44:32

ASP.NET MVC
點贊
收藏

51CTO技術(shù)棧公眾號