asp.net mvc之ActionResult

來源:互聯網
上載者:User

標籤:type   類型   cti   jpeg   encoding   服務   int   permanent   設定   

Web伺服器接收到一個用戶端請求以後,會對請求予以相應,而這個響應是通過Response來控制的,

但是在asp.net mvc 裡,這部分的工作是由ActionResult來完成的,

ActionResult是一個抽象類別,所以具體的工作還是由很多個子類來完成,

具體的子類有

EmptyResult,ContentResult

(通過Content,ContentEncoding,ContentType 分別設定返回的內容,字元編碼格式以及媒體類型),

FileResult(FileContentResult,FilePathResult,FileStreamResult),
<p>Use this area to provide additional information.</p><a href="@Url.Action("ImagePath1", new  { id="1" })">下載</a><img src="@Url.Action("ImagePath1", new  { id="1" })" /><img src="@Url.Action("ImagePath", new  { id="1" })" /><img src="@Url.Action("ImageContent", new  { id="1" })" /><img src="@Url.Action("ImageStream", new  { id="1" })" />

 

        public ActionResult ImagePath(string id)        {            string path = Server.MapPath("/images/" + id + ".jpeg");            return File(path, "image/jpeg");        }        public ActionResult ImagePath1(string id)        {            string path = Server.MapPath("/images/" + id + ".jpeg");            return File(path, "image/jpeg", "下載");        }        public ActionResult ImageContent(string id)        {            string path = Server.MapPath("/images/" + id + ".jpeg");            byte[] heByte = null;            using (FileStream fsRead = new FileStream(path, FileMode.Open, FileAccess.Read))            {                int fsLen = (int)fsRead.Length;                heByte = new byte[fsLen];                int r = fsRead.Read(heByte, 0, heByte.Length);            }            return File(heByte, "image/jpeg");        }        public ActionResult ImageStream(string id)        {            string path = Server.MapPath("/images/" + id + ".jpeg");            FileStream fsRead = new FileStream(path, FileMode.Open, FileAccess.Read);            return File(fsRead, "image/jpeg");        }

 

JavaScriptResult,

返回一段js,且媒體類型是application/x-javascript,

JsonResult,

返回Json資料,預設ContentType為Application/json.

HttpStatusCodeResult,

具體是通過設定response的StatusCode和StatusDescription來完成輸出

RedirectResult,RedirectToRouteResult,

內部是通過Response的Redirect/RedirectPermanent來完成操作,

redirectresult具有兩個屬性permanent和URL,URL可以是絕對的地址也可以是相對位址,permanent決定了重新導向是暫時的還是永久的重新導向,

兩種重新導向的不同點事搜尋引擎會根據永久重新導向來更新自己的索引,

RedirectToRouteResult較RediretResult多了一步根據路由計算出來這個URL值,

所以RedirectToRouteResult沒有URL屬性,卻包含RouteName以及RouteValues屬性,

ViewResult.

 ViewResult是一個特殊的ActionResult,但也是最複雜的一個

asp.net 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.