zoukankan      html  css  js  c++  java
  • ASP.NET MVC 4.0 学习5-ActionResult

    一,Controller簡介

    Controller擔任了資料傳遞的角色,負責流程控制,決定存取哪個Model以及決定顯示哪個View頁面,即ASP.NET MVC中有關於『傳遞』的任務皆由Controller負責。

    Controller的執行階段負責呼叫執行Model中的資料處理,並把處理結果數據傳送到對應的View。

    Controller即為一個Class類,類中包含很多Method方法,方法中進行流程處理。

    Controller有以下特點:

    • 類必須是Public公開類
    • 類名稱必須以Controller結尾
    • 類必須繼承自ASP.NET MVC 的Controller類別
    • Controller中的Action方法必須是Public公開的

    二,Controller執行過程

    STEP1 接受請求

    服務器接收到客戶端的請求

    STEP2 執行路由

    接收請求後,UrlRoutingModule組件逐一對比RouteTable裏面的Route後找到匹配的RouteData,進而根據路由數據決定對應的Controller和Action

    STEP3 MVC請求處理程序

    接著MVCRouteHandler處理器會建立MVCHandler傳送到RequestContext

    STEP4 創建Controller

    MVCHandler使用RequestContext來確認IcontrollerFactory,以用來建立Controller

    STEP5 執行Controller

    MVCHandler調用Controller出來執行

    STEP6 調用Action

    接著ControllerActionInvoker決定要取出Controller裏面的哪個Action

    STEP7 執行Result

    Action方法接受客戶端的輸入來得到結果,並根據回傳類型傳回結果

    當Controller被MVCHandler選擇後,ActionInvoker根據路由參數選取對應的Action.

    Ctrl+F5運行程序以後我們看到首頁:http://localhost:64570/

     

    根據RouteConfig.cs中默認路由配置:

      routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );

    http://localhost:64570/ 根據上面的的默認路由配置把默認值補充完整就是http://localhost:64570/Home/Index,運行結果我們看到了index首頁

     三、Action選擇器

    3-1:名稱選取器

    運行專案,默認的名稱選擇器選擇Action,會取得HomeController中的Index:

        public ActionResult Index()
            {
                ViewBag.Message = "修改此範本即可開始著手進行您的 ASP.NET MVC 應用程式。";
    
                return View();
            }

    現在我們在Action方法上面加上新的ActionName,添加對應新的View,如下

    //添加ActionName
            [ActionName("NewActionName")]
            public ActionResult Index()
            {
                ViewBag.Message = "修改此範本即可開始著手進行您的 ASP.NET MVC 應用程式。";
    
                return View();
            }

    運行以後默認首頁會顯示無法找到此頁面http://localhost:64570/home/index

    修改請求的Action地址為:http://localhost:64570/home/NewActionName  頁面正常顯示為我們剛才添加的View頁面:

    3-2:方法選擇器

    1,NonAction屬性

    Asp.net MVC中會把Controller中所有公開方法視為Action,如果我們想在Controller中建立包括公開屬性的不作為Action用途方法,可以通過NonAction屬性標記該方法:

        [NonAction]
            public ActionResult Function()
            {
                ViewBag.Message = "修改此範本即可開始著手進行您的 ASP.NET MVC 應用程式。";
    
                return View();
            }

    2,Http Action

    Action 方法前加 Http Action屬性後,不同的Http請求(HttpPost,HttpGet,HttpHead,HttpPut...)就會跳轉到不同的Action方法。

    例如:在Index前加上[HttpPost]後,只有post請求才會跳轉到此Action

         //[HttpPost]
            public ActionResult Index()
            {
                ViewBag.Message = "修改此範本即可開始著手進行您的 ASP.NET MVC 應用程式。";
    
                return View();
            }

    3-3:ActionResult

    ActionResult是Action執行以後回傳的數據類型,就相當於方法中返回值是Void或者String,ActionResult的各種返回類型如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    using System.Text;
    using System.IO;
    
    namespace MvcApplication3.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                ViewBag.Message = "修改此範本即可開始著手進行您的 ASP.NET MVC 應用程式。";
    
                return View();
            }
    
            //添加ActionName
            //[ActionName("NewActionName")]
            //public ActionResult Index()
            //{
            //    ViewBag.Message = "修改此範本即可開始著手進行您的 ASP.NET MVC 應用程式。";
    
            //    return View();
            //}
    
            //添加ActionName
            //[NonAction]
            //public ActionResult Function()
            //{
            //    ViewBag.Message = "修改此範本即可開始著手進行您的 ASP.NET MVC 應用程式。";
    
            //    return View();
            //}
    
            //[HttpPost]
            //public ActionResult Index()
            //{
            //    ViewBag.Message = "修改此範本即可開始著手進行您的 ASP.NET MVC 應用程式。";
    
            //    return View();
            //}
    
            ////-------------------------------以下是Action方法的各种回传类型--------------------
    
            //1,ContextResult,回傳純文字文件
            //public ContentResult About()
            //{
            //
            //    return Content("这是ContentResult的回传内容");
            //}
    
            //2,EmptyResult 回傳null
            //public EmptyResult About()
            //{
            //    return null;
            //}
    
            //3,RedirectResult ,相當於Response.Redirect導向新的鏈接
            //public RedirectResult About()
            //{
            //    //根据网址URL进行导向,可以链接到外部的网页
            //    //return Redirect("~/Home/Index");
            //    return Redirect("http://www.baidu.com");
            //}
    
            //4,RedirectToRouteResult,根據路由進行導向
            //public RedirectToRouteResult About()
            //{
            //     只能導向一個專案下面的Controller Action
            //    return RedirectToAction("Index", "Home", null);
            //}
    
            //5, ViewResult 回傳一個檢視結果
            //public ViewResult About()
            //{
            //    //所載入的View可以進行母板頁面的選擇或者設置無模板頁面
            //    return View();
            //}
    
            //6,PartialViewResult 回傳部分檢視,不能加載模板頁面
            //public PartialViewResult About()
            //{
            //    return PartialView();
            //}
    
            //7,回傳一個未經授權的錯誤
            //public HttpUnauthorizedResult About()
            //{
            //    return new HttpUnauthorizedResult();
    
            //    //webConfig中配置了未經授權錯誤發生的時候,重新導向的Url,如下
            //    //  <authentication mode="Forms">
            //    //  <forms loginUrl="~/Account/Login" timeout="2880" />
            //    //</authentication>
            //}
    
            //8,HttpNotFoundResult
            //public HttpNotFoundResult About()
            //{
            //    return HttpNotFound("Page No Found");
            //}
    
            //9,JavaScriptResult,本質上是回傳文字內容,知識Response.ContentType被定義為application/x-javascript
            //public JavaScriptResult About()
            //{
            //    string js = "alert("這是JavaScriptResult回傳結果")";
            //    return JavaScript(js);
            //}
    
            //10,JsonResult,ASP.NET MVC將Response.ContentType定義為application/json 
            //    並通過JavaScriptSerializer把回傳的物件序列化成Json字串
            //public JsonResult About()
            //{
            //    var jsonContent = new { 
            //    Id=1,
            //    Text="這是JsonResult內容"
            //    };
            //    return Json(jsonContent,JsonRequestBehavior.AllowGet);
            //}
    
            //11,FilePathResult,通過路徑傳送檔案到客戶端
            //public FilePathResult About()
            //{
            //    //Server的MapPath方法產生指定的檔案路徑
            //    var imagePath = Server.MapPath("../Images/example.jpg");
            //    //回傳檔案
            //    return File(imagePath, "image/jpeg");
            //}
    
            //12, FileContentResult , 回傳檔案內容,通過二進制的方式進行傳遞
            //  範例中,把字符串轉換成編碼資料,然後傳入File方法中進行回傳
            //public FileContentResult About()
            //{
            //    //使用Encoding把資料以UTF8編碼轉換
            //    byte[] data = Encoding.UTF8.GetBytes("FileContentResult範例");
            //    //File(byte[] 檔案資料,檔案類型,下載時檔案名稱)
            //    return File(data,"text/plain","example.txt");
            //}
    
            //13,FileStreamResult 回傳檔案內容,通過Stream的方式進行傳遞
            //  範例中,用Server的MapPath方法產生指定檔案的url,然後開啟檔案的FileStream傳入File方法中進行回傳
            //public FileStreamResult About()
            //{
            //    //獲取檔案路徑
            //    var path = Server.MapPath("../Images/example.txt");
            //    //使用FileStream開啟檔案
            //    var fileStream = new FileStream(path, FileMode.Open);
            //    //File方法回傳
            //    return File(fileStream,"text/plain","example.txt");
            //}
    
            //14,ActionResult,上述所有的類別都直接或間接繼承自ActionResult,所以上述方法都可以用在ActionResult上
            //所以ActionResult回傳結果比較廣泛
            //public ActionResult About()
            //{
            //    if (new Random().Next()%2==0)
            //    {
            //        //回傳ViewResult
            //        return View();
            //    }
            //    else
            //    {
            //        //回傳JsonResult
            //        return Json(null,JsonRequestBehavior.AllowGet);
            //    }
            //}
    
            public ActionResult Contact()
            {
                ViewBag.Message = "您的連絡頁面。";
    
                return View();
            }
        }
    }
  • 相关阅读:
    .net日期类与UNIX时间戳的相互转换,长数字
    钉钉的生日模块在哪
    js判断手机是苹果(IOS)还是安卓(android) H5手机端自适应宽高
    .net网站部署winserver2008R2 IIS只列出目录 浏览只显示目录浏览
    ajax有时请求不到数据 后台,有时收不到返回值的解决办法
    overflow不超出时不显示滚动条 属性解决内容未超出依然显示滚动条轨道的问题
    PB取datawindow生成的语句。要在datawindow的sqlpreview事件
    电脑C盘缓存路径在哪,清理C盘哪个文件夹可以删
    PB里执行写SQL语句
    SQL SERVER合并行。将多行数据合并成一行,字符串拼接
  • 原文地址:https://www.cnblogs.com/hishanghai/p/4003295.html
Copyright © 2011-2022 走看看