Asp.net MVC 之ActionResult
ActionResult 派生出以下子類:
- ViewResult
返回一個網頁視圖
- PartialViewResult
返回一個網頁視圖,但不適用布局頁。
- ContentResult
返回一段字符串文本。和直接返回string字符串沒有區別,只不過可以設置返回內容的格式和編碼格式。例如:
public string Content()
{
return "<h1>HelloKitty</h1>"; //瀏覽器顯示 HelloKitty
}
? ?
public ActionResult Content2()
{
//return Content("<h1>GoodbyeKitty</h1>"); //
//指定返回文本的格式與字符編碼
return Content("<h1>GoodbyeKitty</h1>",
"text/html",System.Text.Encoding.UTF8);
}
? ?
? ?
- JsonResult
傳入一個任意類型的對象,盡可能地將它格式化為JSON格式。
對於文件或圖片類型數據會轉換為亂碼。
Dictionary這種鍵值對類型會被轉換成js類,List這類會被轉換成js數組
例如:
class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
? ?
public ActionResult JSON1()
{
var array = new List<Student>();
array.Add(new Student { Name = "小明", Age = 12 });
array.Add(new Student { Name = "小李", Age = 15 });
return Json(array,JsonRequestBehavior.AllowGet);
//JsonRequestBehavior用於指定是否允許GET方式訪問,默認只允許POST
? ?
//運行結果:[{"Name":"小明","Age":12},{"Name":"小李","Age":15}]
}
? ?
public ActionResult JSON2()
{
//也可使用匿名內部類來保存數據
return Json(new { name = "test", age = 16, sex = "boy" }, JsonRequestBehavior.AllowGet);
? ?
//運行結果:{"name":"test","age":16,"sex":"boy"}
}
- JavaScriptResult
返回一個JavaScript代碼字符串。JavaScript()的效果實際上和Content()是一樣的,只不過JavaScript()會自動指定返回文本的內容是application/x-javascript。例如:
public ActionResult JS()
{
return JavaScript("alert(‘" + DateTime.Now.ToLongTimeString() + "‘)");
}
? ?
public ActionResult JS2()
{
return Content("alert(‘" + DateTime.Now.ToLongTimeString() + "‘)", "application/x-javascript");
//這樣寫效果和上面完全是一樣的
}
? ?
/*
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="http://localhost:5695/home/js2"></script>
<!--可以直接在script標簽中填寫action的地址-->
</head>
<body></body>
</html>
*/
? ?
- FileResult
返回一個文件,可以通過文件名、文件流、二進制Byte[ ]的形式發送文件,需要指定文件類型。例如:
public ActionResult FILE1()
{
System.IO.Stream fs = System.IO.File.OpenRead(@"test.png");
return File(fs, @"image/png"); //通過流的方法
}
? ?
public ActionResult FILE2()
{
return File(@"test.png", @"image/png"); //通過文件名的方式
}
? ?
public ActionResult FILE3()
{
System.Drawing.Bitmap b = new System.Drawing.Bitmap(100, 100); //創建一張空白圖片
System.IO.MemoryStream ms = new System.IO.MemoryStream();
b.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] bytes = ms.GetBuffer();
? ?
return File(bytes, @"image/bmp"); //通過二進制數據的方式
}
? ?
/*
//現在可以直接將地址賦值給img標簽來進行顯示了
<img src="http://localhost:5695/home/file1" >
*/
? ?
- EmptyResult
返回null,如果Action返回null,則會自動將null轉換為EmptyResult
- RedirectResult
使客戶端瀏覽器跳轉到指定的URL
- RedirectToRouteResult
RedirectToAction()方法將客戶端瀏覽器跳轉的指定的Action
RedirectToRoute()方法將客戶端瀏覽器跳轉到指定的URL,取決於路由
? ?
? ?
附錄:MIME
MIME (Multipurpose Internet Mail Extensions)多用途互聯網郵件擴展類型。
是設定某種擴展名的文件用一種應用程序來打開的方式類型,當該擴展名文件被訪問的時候,瀏覽器會自動使用指定應用程序來打開。
常見文件格式
文件類型 | 格式編碼 |
超文本標記語言文件(.html) | text/html |
XML文檔(.xml) | Text/xml |
普通文本(.txt) | Text/plain |
PDF文檔(.pdf) | application/pdf |
Word文檔(.docx) | application/msword |
PNG圖像(.png) | image/png |
GIF圖形(.gif) | image/gif |
JPEG圖形(.jpeg , .jpg) | image/jpeg |
? ?
? ?
? ?
Asp.net MVC 之ActionResult