MVC之ActionResult

來源:互聯網
上載者:User

標籤:style   blog   http   color   java   io   ar   資料   art   

一、所有的Controller都繼承自System.Web.Mvc.Controller

  目前ASP.NET MVC3預設提供了多種ActionResult的實現,在System.Web.Mvc命名空間裡。

  其中ActionResult是一個抽象類別,所有一下的Result都繼承自它,因此如果一個Action的傳回值是ActionResult的話,可以返回以下任意一種類型的值,但是如果限制死了傳回值為以下任意一種Result,則只能夠返回指定的類型的資料了。

  • ContentResult
  • EmptyResult
  • FileResult
  • HttpStatusCodeResult
  • HttpNotFoundResult
  • HttpUnauthorizedResult
  • JavaScriptResult
  • JsonResult
  • RedirectResult
  • RedirectToRouteResult
  • ViewResultBase
  • PartialViewResult
  • ViewResult
        public ContentResult Index()        {            return Content("測試");       //瀏覽器顯示測試        }        public EmptyResult Index()        {            return new EmptyResult();     //瀏覽器顯示空白                    }        public FileResult Index()        {            return File(Server.MapPath("~/demo.jpg"), "application/x-jpg", "demo.jpg");        //瀏覽器直接下載demo.jpg           }        public HttpNotFoundResult Index()        {            return HttpNotFound();     //報404錯誤                  }        public HttpUnauthorizedResult Index()        {            return new HttpUnauthorizedResult();     //未授權的頁面,跳轉到/Account/LogOn                  }        public JavaScriptResult hello()        {            string js = "alert(‘你還好嗎?‘);";            return JavaScript(js);      //頁面顯示 alert(‘你還好嗎?‘);} 並不會執行這個js,要執行這個js可以在任意視圖裡<script src="@Url.Action("hello")" type="text/javascript"></script>             }        public JsonResult Index()        {            var jsonObj = new            {                Id = 1,                Name = "小銘",                Sex = "男",                Like = "足球"            };            return Json(jsonObj, JsonRequestBehavior.AllowGet);     //返回一個JSON,可以將此代碼輸出到JS處理展示        }        public RedirectResult Index()        {            return Redirect("~/demo.jpg");      //可以跳轉到任意一個路徑            return Redirect("http://www.baidu.com");            return Redirect("/list");        }        public RedirectToRouteResult Index()        {            return RedirectToRoute(     //跳轉到指定Action            new            {                controller = "Home",                action = "GetName"            });        }        public ViewResult Index()        {            return View();          //這個是最常用的,返回指定視圖            //return View("List");            //return View("/User/List");        }        public PartialViewResult Index()        {            return PartialView();          //部分視圖,可以作為一個部分引入另外一個視圖中,跟View大致相同        }

MVC之ActionResult

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.