日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
ASP.NETMVC3教程之控制器與視圖

I:控制器和視圖的基礎(chǔ)概念

公司專注于為企業(yè)提供網(wǎng)站設(shè)計(jì)制作、網(wǎng)站制作、微信公眾號開發(fā)、商城網(wǎng)站建設(shè),小程序定制開發(fā),軟件按需網(wǎng)站制作等一站式互聯(lián)網(wǎng)企業(yè)服務(wù)。憑借多年豐富的經(jīng)驗(yàn),我們會(huì)仔細(xì)了解各客戶的需求而做出多方面的分析、設(shè)計(jì)、整合,為客戶設(shè)計(jì)出具風(fēng)格及創(chuàng)意性的商業(yè)解決方案,成都創(chuàng)新互聯(lián)更提供一系列網(wǎng)站制作和網(wǎng)站推廣的服務(wù)。

  1.控制器的概念

  控制器在ASP.NET MVC3當(dāng)中是最終處理客戶端請求的一個(gè)實(shí)現(xiàn),他有個(gè)一硬性條件就是必須實(shí)現(xiàn)System.Web.Mvc.IController接口,而且類的命名必須要以Controller結(jié)尾,盡管按照硬性條件上來說,要自己實(shí)現(xiàn)一個(gè)接口比較糾結(jié),但幸好的是在ASP.NET MVC3內(nèi)部已經(jīng)包含了一個(gè)默認(rèn)的實(shí)現(xiàn),我們只需要把類的命名設(shè)置為Controller結(jié)尾然后繼承System.Web.Mvc.Controller類,立刻就可以輕松地實(shí)現(xiàn)了IController接口了.如果你不喜歡這種默認(rèn)提供的實(shí)現(xiàn)方式,而是自己去實(shí)現(xiàn)IController的話.下面的代碼可以做一個(gè)簡單的參考:

 
 
 
 
  1.   usingSystem.Web.Mvc;  
  2.   usingSystem.Web.Routing;  
  3.   namespaceMvcApplication1.Controllers  
  4.   {  
  5.   publicclassNoDefaultController: IController  
  6.   {  
  7.   voidIController.Execute(RequestContextrequestContext)  
  8.   {  
  9.   varhttpContext = requestContext.HttpContext;  
  10.   varresponse = httpContext.Response;  
  11.   response.ContentType = "text/html; charset=utf-8";  
  12.   response.Write("自己的簡單實(shí)現(xiàn)! Hello World");  
  13.   }  
  14.   }  
  15.   } 

  請注意:控制器類不硬性要求放在*.Controllers命名空間中

如果是使用繼承默認(rèn)的類去實(shí)現(xiàn)的話代碼如下:

 
 
 
 
  1.   usingSystem.Web.Mvc;  
  2.   namespaceMvcApplication1  
  3.   {  
  4.   publicclassHelloController: Controller  
  5.   {  
  6.   publicActionResultIndex()  
  7.   {  
  8.   returnContent("默認(rèn)的實(shí)現(xiàn)! Hello World");  
  9.   }  
  10.   }  
  11.   } 

  效果圖我就不發(fā)了.大家可以手動(dòng)試一試.

  2.視圖的概念

  視圖的理解比較簡單.你可以把視圖理解為*.aspx或*.cshtml的文件.但是不是所有的aspx, cshtml文件都可以作為視圖,它們必須得屬于某一個(gè)以控制器名稱命名的文件夾當(dāng)中,而且要按照約定去存放到路徑 ~/Views/{controller}/View.cshtml.

  另外視圖還包含解析他的ViewEngine(視圖引擎),本文在這里不涉及這個(gè)高級話題.

  我們可以看出在~/Views/{controller}/View.cshtml中的{controller}并不需要像類命名那樣以Controller結(jié)尾去做文件夾名稱.

  3.工作原理圖

  當(dāng)然,內(nèi)部的工作原理會(huì)比這個(gè)圖復(fù)雜得多.在此也僅僅是讓大家有個(gè)了解而已!如有錯(cuò)誤請指出.謝謝!

  提示:MvcHandler實(shí)現(xiàn)了IHttpAsyncHandler, IHttpHandler, IRequiresSessionState這三個(gè)接口,我在Debug進(jìn)去的時(shí)候發(fā)現(xiàn)都是走異步的路線.對于這個(gè)處理方式,了解的人希望可以為我解答一下!

  II:控制器

  1.操作方法

  操作方法的含義是指在繼承了System.Web.Mvc.Controller類中所定義的返回值的類型可以兼容ActionResult的方法.

 
 
 
 
  1.   usingSystem.Web.Mvc;  
  2.   namespaceMvcApplication1.Controllers  
  3.   {  
  4.   publicclassHomeController: Controller  
  5.   {  
  6.   ///  
  7.  ///Hi, 我是Index操作方法  
  8.   ///  
  9.   ///  
  10.   publicActionResultIndex()  
  11.   {  
  12.  ViewBag.Message = "Welcome to ASP.NET MVC!";  
  13.   returnView();  
  14.   }  
  15.   ///  
  16.   ///厄, 我是About操作方法  
  17.   ///  
  18.   ///  
  19.   publicActionResultAbout()  
  20.   {  
  21.   returnView();  
  22.   }  
  23.   ///  
  24.   ///可以為方法添加不是操作方法的特性  
  25.   ///  
  26.   ///  
  27.   [NonAction]  
  28.   publicstringNonAction()  
  29.   {  
  30.   return"親,不好意思噢.我不是操作方法噢,請不要亂調(diào)用噢!";  
  31.   }  
  32.   }  
  33.   }  
  34.   同時(shí)也可以使用[ActionName(“重命名操作方法”)]特性去重命名操作方法.  
  35.   [ActionName("NewActionName")]  
  36.   publicActionResultRenameAction()  
  37.   {  
  38.   returnContent("利用特性換個(gè)馬甲");  
  39.   } 

2.操作方法的返回值類型的種類

  目前ASP.NET MVC3默認(rèn)提供了11種ActionResult的實(shí)現(xiàn)

  在System.Web.Mvc命名空間

  ActionResult

  ContentResult

  EmptyResult

  FileResult

  HttpStatusCodeResult

  HttpNotFoundResult

  HttpUnauthorizedResult

  JavaScriptResult

  JsonResult

  RedirectResult

  RedirectToRouteResult

  ViewResultBase

  PartialViewResult

  ViewResult

  代碼示例:

 
 
 
 
  1.   usingSystem.Web.Mvc;  
  2.   namespaceMvcApplication1.Controllers  
  3.   {  
  4.   publicclassActionResultController: Controller  
  5.   {  
  6.   publicActionResultIndex()  
  7.   {  
  8.   returnView();  
  9.   }  
  10.   publicActionResultContentResult()  
  11.  {  
  12.   returnContent("Hi, 我是ContentResult結(jié)果");  
  13.   }  
  14.   publicActionResultEmptyResult()  
  15.   {  
  16.   //空結(jié)果當(dāng)然是空白了!  
  17.   //至于你信不信, 我反正信了  
  18.   returnnewEmptyResult();  
  19.   }  
  20.   publicActionResultFileResult()  
  21.   {  
  22.   varimgPath = Server.MapPath("~/demo.jpg");  
  23.   returnFile(imgPath, "application/x-jpg", "demo.jpg");  
  24.   }  
  25.  publicActionResultHttpNotFoundResult()  
  26.   {  
  27.   returnHttpNotFound("Page Not Found");  
  28.   }  
  29.   publicActionResultHttpUnauthorizedResult()  
  30.   {  
  31.   //未驗(yàn)證時(shí),跳轉(zhuǎn)到Logon  
  32.   returnnewHttpUnauthorizedResult();  
  33.   }  
  34.   publicActionResultJavaScriptResult()  
  35.   {  
  36.   stringjs = "alert(\"Hi, I'm JavaScript.\");";  
  37.   returnJavaScript(js);  
  38.   }  
  39.   publicActionResultJsonResult()  
  40.   {  
  41.   varjsonObj = new 
  42.   {  
  43.   Id = 1,  
  44.   Name = "小銘",  
  45.   Sex = "男",  
  46.   Like = "足球" 
  47.   };  
  48.   returnJson(jsonObj, JsonRequestBehavior.AllowGet);  
  49.   }  
  50.   publicActionResultRedirectResult()  
  51.  {  
  52.   returnRedirect("~/demo.jpg");  
  53.   }  
  54.   publicActionResultRedirectToRouteResult()  
  55.   {  
  56.   returnRedirectToRoute(new{  
  57.   controller = "Hello", action = "" 
  58.   });  
  59.   }  
  60.   publicActionResultViewResult()  
  61.   {  
  62.   returnView();  
  63.   }  
  64.   publicActionResultPartialViewResult()  
  65.   {  
  66.   returnPartialView();  
  67.   }  
  68.   //禁止直接訪問的ChildAction  
  69.   [ChildActionOnly]  
  70.   publicActionResultChildAction()  
  71.   {  
  72.   returnPartialView();  
  73.   }  
  74.   //正確使用ChildAction  
  75.   publicActionResultUsingChildAction()  
  76.   {  
  77.   returnView();  
  78.   }  
  79.   }  
  80.   } 

  請注意,個(gè)別的操作方法結(jié)果在執(zhí)行時(shí),他們返回的HTTP狀態(tài)碼及ContentType有差別的.~另外如果要知道ContentType到底有多少種設(shè)置可參考

  3.操作方法的參數(shù)

  在本小節(jié),我僅僅演示如何使URL參數(shù)映射到操作方法的參數(shù),對于更復(fù)雜的用法,我將會(huì)留到 模型 的章節(jié)去講解.

  首先我們需要先添加一個(gè)新的路由映射,然后在設(shè)置3個(gè)占位符參數(shù),它們分別是p1, p2, p3.然后將p1約束為僅字母與數(shù)字的組合,p2約束為僅數(shù)字,p3沒有添加約束.

 
 
 
 
  1.   routes.MapRoute(  
  2.   "UsingParams",  
  3.   "p/{p1}/{p2}/{p3}",  
  4.   new{  
  5.   controller = "Home",  
  6.   action = "UsingParams" 
  7.   },  
  8.   new{ p1 = "[a-z0-9]+", p2 = @"d+"}  
  9.   ); 

  在添加一個(gè)Home控制器的操作方法

 
 
 
 
  1.   publicActionResultUsingParams(stringp1, intp2, stringp3)  
  2.   {  
  3.   stringoutput = string.Empty;  
  4.   output += "p1 = "+ (p1 ?? "null");  
  5.   output += "p2 = " 
  6.   + (p2.HasValue p2.Value.ToString() : "沒有值");  
  7.   output += "p3 = "+ (p3 ?? "null");  
  8.   returnContent(output);  
  9.   } 

  運(yùn)行效果

這里在弄一個(gè)仿YouKu的URL路由設(shè)置

路由設(shè)置

 
 
 
 
  1.   routes.MapRoute(  
  2.   "YouKu_Show",  
  3.   "v_{action}/id_{id}.html",  
  4.   new{ controller = "YouKu"},  
  5.   new{ id = "[a-z0-9]{13}"},  
  6.   newstring[] { "MvcApplication1.YouKu"}  
  7.   );  
  8.   routes.MapRoute(  
  9.   "YouKu_PlayList",  
  10.   "v_{action}/{id}.html",  
  11.   new{ controller = "YouKu"},  
  12.   new{ id = "[a-z0-9]{12}"},  
  13.   newstring[] { "MvcApplication1.YouKu"}  
  14.   ); 

  詳細(xì)代碼文末會(huì)放出.

  III:視圖

  1.視圖的語法在很早的時(shí)候我已經(jīng)寫過一篇文章了.在此就略過不提.

  2.視圖與控制器之間如何進(jìn)行數(shù)據(jù)交互

  在前面的接觸當(dāng)中,我們已經(jīng)對控制器和視圖有一定的了解了.接著,我們將要了解他們之間的幾種常用的數(shù)據(jù)交互方式.注意:ASP.NET MVC不存在IsPostBack.如果你需要把WebForm和MVC相結(jié)合.那么很抱歉,我個(gè)人非常反對這種方式.因?yàn)檫x擇MVC的主要原因就是不希望再與runat=server打交道(當(dāng)然你用ASP.NET開發(fā)而不去runat=server的話,是可以實(shí)現(xiàn)的).另一點(diǎn)MVC也方便測試.~在過去,如果你要對ASP.NET進(jìn)行測試,我們可以設(shè)想一下,對每個(gè)需要測試的runat=server的服務(wù)器控件去設(shè)置一個(gè)預(yù)設(shè)值的話,由于屬性繁多,復(fù)雜程度已經(jīng)可想而知了.另外從根源上并不能保證都能發(fā)現(xiàn)所有問題.~或許我的這個(gè)測試ASP.NET的猜想根本不成立.而測試的時(shí)候往往還需要每Builder一次,然后對需要測試的頁面逐個(gè)測試檢查什么按鈕之類的..OK,這些傷心事就不在提了.下面介紹下MVC下的數(shù)據(jù)交互有那幾種方式.

  2.1 ASP.NET MVC不在有IsPostBack,如何實(shí)現(xiàn)處理GET,POST?

  首先我帖出一段簡單的代碼去為你展示ASP.NET MVC3下處理GET, POST的方式

 
 
 
 
  1.   //默認(rèn)是處理Get請求,當(dāng)然你也可以顯式添加  
  2.   [HttpGet]  
  3.   publicActionResultUsingViewBag()  
  4.   {  
  5.   returnView();  
  6.  }  
  7.   //顯式將操作方法設(shè)置處理Post請求  
  8.   [HttpPost]  
  9.   publicActionResultUsingViewBag(stringinput)  
  10.   {  
  11.   if(string.IsNullOrWhiteSpace(input))  
  12.   {  
  13.   ViewBag.Msg = inputBlank;  
  14.   }  
  15.   else 
  16.   {  
  17.   ViewBag.Msg = "你輸入了: "+ input;  
  18.   }  
  19.   returnView();  
  20.   } 

  在這里你會(huì)發(fā)現(xiàn)在ASP.NET MVC下是用[Http*]或[AcceptVerbs(HttpVerbs.*)]特性去實(shí)現(xiàn)類似WebForm下的IsPostBack.

  2.2 ASP.NET MVC3的數(shù)據(jù)交互方式種類

  A:ASP.NET原生的Request,Response.

  System.Web.Mvc.Controller的成員:HttpContext, Request, Response, Session, User都跟WebForm下的類似.

  Request.QueryString,Request.Form,Request.Cookies,RouteData.Values等.

  B:ASP.NET MVC3自帶的ViewData,ViewBag,TempData

 
 
 
 
  1.   usingSystem.Web.Mvc;  
  2.   namespaceMvcApplication1.Controllers  
  3.   {  
  4.   publicclassParamsController: Controller  
  5.   {  
  6.   stringinputBlank = "你輸入了空白";  
  7.  publicActionResultIndex()  
  8.   {  
  9.   returnView();  
  10.   }  
  11.   //默認(rèn)是處理Get請求,當(dāng)然你也可以顯式添加  
  12.   [HttpGet]  
  13.   publicActionResultUsingViewBag()  
  14.   {  
  15.   returnView();  
  16.   }  
  17.   //顯式將操作方法設(shè)置處理Post請求  
  18.   [HttpPost]  
  19.   publicActionResultUsingViewBag(stringinput)  
  20.   {  
  21.   if(string.IsNullOrWhiteSpace(input))  
  22.   {  
  23.   ViewBag.Msg = inputBlank;  
  24.   }  
  25.   else 
  26.   {  
  27.   ViewBag.Msg = "你輸入了: "+ input;  
  28.   }  
  29.   returnView();  
  30.   }  
  31.   publicActionResultUsingViewData()  
  32.   {  
  33.   returnView();  
  34.   }  
  35.   [HttpPost]  
  36.   publicActionResultUsingViewData(stringinput)  
  37.   {  
  38.   if(string.IsNullOrWhiteSpace(input))  
  39.   {  
  40.   ViewData["msg"] = inputBlank;  
  41.   }  
  42.   else 
  43.   {  
  44.   ViewData["msg"] = "你輸入了: "+ input;  
  45.   }  
  46.   returnView();  
  47.   }  
  48.   publicActionResultUsingTempData()  
  49.   {  
  50.   returnView();  
  51.  }  
  52.   [HttpPost]  
  53.   publicActionResultUsingTempData(stringinput)  
  54.   {  
  55.   if(string.IsNullOrWhiteSpace(input))  
  56.   {  
  57.   TempData["msg"] = inputBlank;  
  58.   }  
  59.   else 
  60.   {  
  61.   TempData["msg"] = "你輸入了: "+ input;  
  62.   }  
  63.  returnView();  
  64.   }  
  65.   }  } 

  更詳細(xì)的討論,或許得另寫一遍文章了.下一篇寫Model,另外關(guān)于@Html的擴(kuò)展方法,我打算另外寫一篇去介紹.

  IV:源代碼下載

原文:http://www.cnblogs.com/highend/archive/2011/08/04/aspnet_mvc3_controller_and_view.html


網(wǎng)頁題目:ASP.NETMVC3教程之控制器與視圖
地址分享:http://www.5511xx.com/article/coscsgi.html