ASP.NET MVC 3 Controller


上兩篇文章講的是View層的Razor視圖引擎,那么今天咱就講另一個玩玩,什么呢 ? Controller

首先看一下ASP.net MVC 的請求過程 看一下客戶端向服務器發送一個請求后服務器 做了哪些事情吧!

有些內容圖上我已經標的很清楚了,我再這就不再一一描述了,直接看圖就OK了。

下面是今天要說的Controller

      從上圖可以看出客戶端請求通過IIS已經MVC中路由機制的解析后就扔給MVC 的controller處理了,從這點可以看出controller(控制器)在MVC中扮演着處理客戶端請求的角色。

對於controller有以下幾個注意事項:

> 1. 必須實現System.Web.Mvc.IController 接口 --- 但是正常情況小我們是直接繼承System.Web.Mvc.Controller 類 ,這樣就避免了我們再完全的實現IController接口了,但是如果您的項目需求現有的controller類不能滿足那就自己實現一下IController 接口吧!

> 2. 必須要以Controller結尾, 比如 有這么個url地址:http://www.cnblogs/wlitsoft/blogs/123 路由解析后的結果是找wlitsoftController 類。

> 3. Controller 類中有大量的返回值為ActionResult 的方法,這寫方法就是所謂的Action,然后Controller通過不同的Action來處理具體的客戶端請求,比如還是上面的那個url地址,就是wlitSoftController 會找  blogs 這個 Action 方法 然后參數為123。

說到ActionResult 那就不能不好好的說說,好下面咱就好好的說下ActionResult

類名 抽象類 父類 功能
ContentResult     根據內容的類型和編碼,數據內容.
EmptyResult     空方法.
FileResult abstract   寫入文件內容,具體的寫入方式在派生類中.
FileContentResult   FileResult 通過 文件byte[] 寫入文件.
FilePathResult   FileResult 通過 文件路徑 寫入文件.
FileStreamResult   FileResult 通過 文件Stream 寫入文件.
HttpUnauthorizedResult     拋出401錯誤
JavaScriptResult     返回javascript文件
JsonResult     返回Json格式的數據
RedirectResult     使用Response.Redirect重定向頁面
RedirectToRouteResult     根據Route規則重定向頁面
ViewResultBase abstract   調用IView.Render()
PartialViewResult   ViewResultBase 調用父類ViewResultBase 的ExecuteResult方法. 重寫了父類的FindView方法. 尋找用戶控件.ascx文件
ViewResult   ViewResultBase 調用父類ViewResultBase 的ExecuteResult方法. 重寫了父類的FindView方法. 尋找頁面.aspx文件

下面咱就同代碼的方式依次證明驗證一下這些ActionResult
首先,建一個MVC 3 項目 這里就不再演示了,不會的的可以看下我以前寫的文章。

第二步 新建一個Controller

添加一個名為CtextController 的 Controller

第三步 寫代碼唄

  1 namespace Mvc3App1.Controllers
  2 {
  3     public class CTestController : Controller
  4     {
  5         //
  6         // GET: /CTest/
  7 
  8         public ActionResult Index()
  9         {
 10             return View();
 11         }
 12 
 13         public ActionResult ContentResult()
 14         {
 15             return Content("Hi, 我是ContentResult結果");
 16             //return Content("<script>alert('abc')</script>");
 17         }
 18 
 19         public ActionResult EmptyResult()
 20         {
 21             //就是一個空白頁面 真的是一個什么都沒有的頁面
 22             return new EmptyResult();
 23         }
 24 
 25         public ActionResult FileResult()
 26         {
 27             var imgPath = Server.MapPath("~/images/1.jpg");
 28             return File(imgPath, "application/x-jpg", "1.jpg");
 29         }
 30 
 31         public ActionResult HttpNotFoundResult()
 32         {
 33             return HttpNotFound("Page Not Found");
 34         }
 35 
 36         public ActionResult HttpUnauthorizedResult()
 37         {
 38             //未驗證時,跳轉到Logon
 39             return new HttpUnauthorizedResult();
 40         }
 41 
 42         public ActionResult JavaScriptResult()
 43         {
 44             //string js = "alert(\"Hi, I'm JavaScript.\");";
 45             //return JavaScript(js);
 46             string s = "$('#divResultText').html('JavaScript Passed')";
 47             return JavaScript(s);
 48         }
 49 
 50         public ActionResult JsonResult()
 51         {
 52             var jsonObj = new
 53             {
 54 
 55                 Id = 1,
 56 
 57                 Name = "小銘",
 58 
 59                 Sex = "",
 60 
 61                 Like = "足球"
 62 
 63             };
 64             return Json(jsonObj, JsonRequestBehavior.AllowGet);
 65         }
 66 
 67         public ActionResult RedirectResult()
 68         {
 69             return Redirect("~/images/1.jpg");
 70         }
 71 
 72         public ActionResult RedirectToRouteResult()
 73         {
 74             return RedirectToRoute(new
 75             {
 76                 controller = "Hello",
 77                 action = ""
 78             });
 79         }
 80 
 81         public ActionResult ViewResult()
 82         {
 83             return View();
 84         }
 85 
 86         public ActionResult PartialViewResult()
 87         {
 88             return PartialView();
 89         }
 90         //禁止直接訪問的ChildAction
 91         [ChildActionOnly]
 92         public ActionResult ChildAction()
 93         {
 94 
 95             return PartialView();
 96 
 97         }
 98 
 99         //正確使用ChildAction
100         public ActionResult UsingChildAction()
101         {
102 
103             return View();
104 
105         }
106     }

 部分運行結果:

  index 運行結果

contentResult運行結果

 

HttpUnauthorizedResult運行結果

 

FIleResult運行結果

      尋人啟事  : 上面的JavascriptResult 和 JsonResult 兩個 沒有測試成功!我查MSDN 也沒查到為什么!如果有知道此錯誤的請您與我聯系!如果有了解決方法我將及時更新本博文

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM