MVC Controller与ActionResult的返回值
?
Action的要求
? 必須是一個公有方法
? 必須返回ActionResult類型
? 必須是實例方法
? 不能是范型方法
? 沒有標注NonActionAttribute
? 不能被重載(overload)
Controller 提供了眾多的方法讓我們返回各種類型的 ActionResult。
1. View
最常用的一種,用于返回一個 "標準" 頁面。
{
??if (model != null)
??{
????base.ViewData.Model = model;
??}
??return new ViewResult
??{
????ViewName = viewName,
????MasterName = masterName,
????ViewData = base.ViewData,
????TempData = base.TempData
??};
}
public class ViewResult : ViewResultBase
{
??protected override ViewEngineResult FindView(ControllerContext context)
??{
????ViewEngineResult result = ViewEngineCollection.FindView(context, ViewName, MasterName);
????if (result.View != null)
????{
??????return result;
????}
????...
??}
}
這個頁面默認是 ViewPage,也可以是我們自己定義的其它模板引擎頁面。
MVC 還提供了強類型的 ViewPage<TModel>。
{
??public string Name { get; set; }
??public int Age { get; set; }
}
public class TestController : Controller
{
??public ActionResult Index()
??{
????ViewData["message"] = "Hello, World!";
????var model = new User { Name = "Tom", Age = 13 };
????return View(model);
??}
}
Index.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
??<title>Index</title>
</head>
<body>
??Name: <%= Model.Name %>; Age: <%= Model.Age %>
</body>
</html>
在 WebForm 時代,我們就已經習慣了將一個頁面分解成多個 UserControl,現在我們依然可以這么做。htmlHelper 專門提供了 RenderPartial 擴展方法,從當前視圖目錄(Views\xxx)下載入 .ascx 頁面。
{
??public static void RenderPartial(this HtmlHelper htmlHelper, partialViewName, model, viewData)
??{
????htmlHelper.RenderPartialInternal(partialViewName, viewData, model, ViewEngines.Engines);
??}
}
public class HtmlHelper
{
??internal virtual void RenderPartialInternal(string partialViewName, ViewDataDictionary viewData,
????object model, ViewEngineCollection viewEngineCollection)
??{
????...
????ViewDataDictionary newViewData = null;
????if (model == null)
????{
??????if (viewData == null)
????????newViewData = new ViewDataDictionary(ViewData);
??????else
????????newViewData = new ViewDataDictionary(viewData);
????}
????else
????{
??????if (viewData == null)
????????newViewData = new ViewDataDictionary(model);
??????else
????????newViewData = new ViewDataDictionary(viewData) { Model = model };
????}
????ViewContext newViewContext = new ViewContext(ViewContext, ViewContext.View,
??????newViewData, ViewContext.TempData);
????IView view = FindPartialView(newViewContext, partialViewName, viewEngineCollection);
????view.Render(newViewContext, ViewContext.HttpContext.Response.Output);
??}
??internal static IView FindPartialView(viewContext, partialViewName, viewEngineCollection)
??{
????ViewEngineResult result = viewEngineCollection.FindPartialView(viewContext, partialViewName);
????if (result.View != null)
????{
??????return result.View;
????}
????...
??}
}
RenderPartialInternal 調用 FindParitialView 從視圖引擎中載入 .ascx,同時將當前的環境參數傳遞給它。也就是說 RenderPartial 只是一種視圖級別的行為,并不會再次觸發 Controller Action 操作,這點要和 Controller.PartialView() 區別開來。
Index.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
??<title>Index</title>
</head>
<body>
??Name: <%= Model.Name %>; Age: <%= Model.Age %>
??<br />
??<% Html.RenderPartial("Part"); %>
</body>
</html>
Part.ascx
<%= ViewData["message"] %>
<br />
<%= Model.Name %>
2. Content
Content 用于輸出(Response.Write) "靜態" 片段。
{
??return new ContentResult
??{
????Content = content,
????ContentType = contentType,
????ContentEncoding = contentEncoding
??};
}
public class ContentResult : ActionResult
{
??public string Content { get; set; }
????
??public override void ExecuteResult(ControllerContext context)
??{
????...
????HttpResponseBase response = context.HttpContext.Response;
????if (!String.IsNullOrEmpty(ContentType))
????{
??????response.ContentType = ContentType;
????}
????if (ContentEncoding != null)
????{
??????response.ContentEncoding = ContentEncoding;
????}
????if (Content != null)
????{
??????response.Write(Content);
????}
??}
}
看看和 jQuery 的配合使用。
{
??public ActionResult Index()
??{
????return View();
??}
??public ActionResult Part()
??{
????return Content("<a href=\"http://www.rainsts.net\">Q.yuhen</a>");
??}
}
Index.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
??<title>Index</title>
??<script src="http://www.cnblogs.com/Scripts/jquery-1.3.1.min.js" type="text/javascript"></script>
??<script type="text/javascript">
????$(function()
????{
??????$("#div1").load("/test/part");
????});
??</script>
</head>
<body>
??<div id="div1">
??</div>
</body>
</html>
3. PartialView
Controller.PartialView() 和 HtmlHelper.RenderPartial() 的不同之處在于前者是再次執行 ActionInvoke 并返回一個 ActionResult 結果,后者只是使用現有的 ViewContext 顯示一個視圖片段。而與 Controller.Content() 的區別是 PartialView() 使用視圖引擎輸出一個 "動態" 的 ascx 結果。
{
??if (model != null)
??{
????ViewData.Model = model;
??}
??return new PartialViewResult
??{
????ViewName = viewName,
????ViewData = ViewData,
????TempData = TempData
??};
}
public class PartialViewResult : ViewResultBase
{
??protected override ViewEngineResult FindView(ControllerContext context)
??{
????ViewEngineResult result = ViewEngineCollection.FindPartialView(context, ViewName);
????if (result.View != null)
????{
??????return result;
????}
????
????...
??}
}
和 Content() 一樣,我們通常將其和 jQuery 等 Ajax 框架配合使用。
{
??public ActionResult Index()
??{
????return View();
??}
??public ActionResult Part()
??{
????ViewData["time"] = DateTime.Now;
????var model = new User { Name = "Tom", Age = 13 };
????return PartialView(model);
??}
}
Index.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
??<title>Index</title>
??<script src="http://www.cnblogs.com/Scripts/jquery-1.3.1.min.js" type="text/javascript"></script>
??<script type="text/javascript">
????$(function()
????{
??????$("#div1").load("/test/part");
????});
??</script>
</head>
<body>
??<div id="div1">
??</div>
</body>
</html>
Part.ascx
<%= ViewData["time"] %> <br />
<%= Model.Name %>; <%= Model.Age %>
4. Redirect / RedirectToAction / RedirectToRoute
Controller 提供了幾種方式,讓我們在不同的 Action 之間進行跳轉。
{
??public static void RegisterRoutes(RouteCollection routes)
??{
????...
????routes.MapRoute
????(
??????"Test2",
??????"Test/T2/{name}/{age}",
??????new { controller = "Test", action = "T2", name = "", age = 0 }
????);
????...
??}
}
?
方法1:
Redirect() 直接用 Response.Redirect() 完成 url 跳轉。
{
??public ActionResult Index()
??{
????return Redirect("/Test/T2/Tom/23");
??}
??public ActionResult T2(User user)
??{
????return Content(user.Name);
??}
}
相關細節:
{
??...
??return new RedirectResult(url);
}
public class RedirectResult : ActionResult
{
??public override void ExecuteResult(ControllerContext context)
??{
????...
????string destinationUrl = UrlHelper.Content(Url, context.HttpContext);
????context.HttpContext.Response.Redirect(destinationUrl, false /* endResponse */);
??}
}
方法2:
RedirectToAction() 直接使用 Action Name 進行跳轉。
{
??public ActionResult Index()
??{
????return RedirectToAction("T2", new { name = "Tom", age = 23 });
??}
??public ActionResult T2(User user)
??{
????return Content(user.Name);
??}
}
如果目標 Action 不在當前 Controller 類,則可以指定目標 Controller Name。
相關細節:
??string controllerName, RouteValueDictionary routeValues)
{
??RouteValueDictionary mergedRouteValues;
??if (RouteData == null)
??{
????mergedRouteValues = RouteValuesHelpers.MergeRouteValues(actionName,
??????controllerName, null, routeValues, true /* includeImplicitMvcValues */);
??}
??else
??{
????mergedRouteValues = RouteValuesHelpers.MergeRouteValues(actionName,
??????controllerName, RouteData.Values, routeValues, true /* includeImplicitMvcValues */);
??}
??return new RedirectToRouteResult(mergedRouteValues);
}
public class RedirectToRouteResult : ActionResult
{
??public override void ExecuteResult(ControllerContext context)
??{
????...
????string destinationUrl = UrlHelper.GenerateUrl(RouteName, null /* actionName */,
??????null /* controllerName */, RouteValues, Routes, context.RequestContext,
??????false /* includeImplicitMvcValues */);
????...
????context.HttpContext.Response.Redirect(destinationUrl, false /* endResponse */);
??}
}
可以看到 RedirectToRouteResult.ExecuteResult 中使用 Route 相關信息拼接成目標 Url 后進行跳轉。
方法3:
RedirectToRoute() 則是直接用 MapRoute 時定義的 Route Name 進行跳轉。
{
??public ActionResult Index()
??{
????return RedirectToRoute("Test2", new { name = "Tom", age = 23 });
??}
}
相關細節:
{
??return new RedirectToRouteResult(routeName, RouteValuesHelpers.GetRouteValues(routeValues));
}
執行過程和 RedirectToAction() 相同。
5. Json
Json() 在編寫 Ajax 時非常有用,可以將 Entity 等對象序列化成 JSON 格式供 Javascript 使用。
{
??public ActionResult Index()
??{
????return View();
??}
??public ActionResult GetUser(string name)
??{
????var user = new User { Name = name, Age = 23 };
????return Json(user);
??}
}
Index.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
??<title>Index</title>
??<script src="http://www.cnblogs.com/Scripts/jquery-1.3.1.min.js" type="text/javascript"></script>
??<script type="text/javascript">
????$(function()
????{
??????$("#btnTest").click(function()
??????{
????????$.getJSON
????????(
??????????"/Test/GetUser",
??????????{ name: "Tom" },
??????????function(json)
??????????{
????????????alert(json.Name + ";" + json.Age);
??????????}
????????);
??????});
????});
??</script>
</head>
<body>
??<input type="button" id="btnTest" value="Test" />
</body>
</html>
很好用,不是嗎?看看相關細節。
{
??return new JsonResult
??{
????Data = data,
????ContentType = contentType,
????ContentEncoding = contentEncoding
??};
}
public class JsonResult : ActionResult
{
??public override void ExecuteResult(ControllerContext context)
??{
????...
????if (Data != null)
????{
??????JavaScriptSerializer serializer = new JavaScriptSerializer();
??????response.Write(serializer.Serialize(Data));
????}
??}
}
使用 System.Web.Script.Serialization.JavaScriptSerializer 完成 JSON 序列化操作,也就是說我們還可以用 ScriptIgnoreAttribute 排除某些屬性。
6. Javascript
某些時候,我們需要根據一些邏輯判斷來載入執行不同的 Javascript 代碼。
{
??public ActionResult Index()
??{
????return View();
??}
??public ActionResult GetJs(int id)
??{
????switch (id)
????{
??????case 1:
????????return JavaScript("alert('Hello, C#!');");??????
??????case 2:
????????return JavaScript("alert('Hello, MVC!');");??????
??????default:
????????return null;
????}
??}
}
Index.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
??<title>Index</title>
??<script src="http://www.cnblogs.com/Scripts/jquery-1.3.1.min.js" type="text/javascript"></script>
??<script type="text/javascript">
????$(function()
????{
??????$("#btnTest").click(function()
??????{
????????var id = $("#txtId").val();
????????$.getScript("/Test/GetJs/" + id);
??????});
????});
??</script>
</head>
<body>
??<input type="text" id="txtId" value="1" />
??<input type="button" id="btnTest" value="Test" />
</body>
</html>
只是這種做法,似乎將 View 和 Controller 的耦合加大了…… 還不如直接用 Javascript 來處理這些。
{
??return new JavaScriptResult { Script = script };
}
public class JavaScriptResult : ActionResult
{
??public override void ExecuteResult(ControllerContext context)
??{
????...
????HttpResponseBase response = context.HttpContext.Response;
????response.ContentType = "application/x-javascript";
????if (Script != null)
????{
??????response.Write(Script);
????}
??}
}
7. File (Download / Upload)
File() 提供了 Download 功能。
{
??public ActionResult Index()
??{
????return View();
??}
??public ActionResult Download(int id)
??{
????var filename = String.Format("~/Content/Download/{0}.rar", id);
????var fileDownloadName = String.Format("{0}.rar", id);
????return File(filename, "application/octet-stream", fileDownloadName);
??}
}
當我們在瀏覽器請求 "/Test/Download/1" 是就會打開下載窗口,同時給出了保存文件名。
{
??return new FileContentResult(fileContents, contentType) { FileDownloadName = fileDownloadName };
}
public abstract class FileResult : ActionResult
{
??public override void ExecuteResult(ControllerContext context)
??{
????...
????HttpResponseBase response = context.HttpContext.Response;
????response.ContentType = ContentType;
????...
????WriteFile(response);
??}
??protected abstract void WriteFile(HttpResponseBase response);
}
public class FileContentResult : FileResult
{
??protected override void WriteFile(HttpResponseBase response)
??{
????response.OutputStream.Write(FileContents, 0, FileContents.Length);
??}
}
文件上傳是另一個常用的 Web 應用。
{
??public ActionResult Index()
??{
????return View();
??}
??public ActionResult Upload(HttpPostedFileBase file)
??{
????var filename = Server.MapPath("~/Content/Upload/" + Path.GetFileName(file.FileName));
????file.SaveAs(filename);
????return null;
??}
}
Index.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
??<title>Index</title>
</head>
<body>
??<form action="/Test/Upload" enctype="multipart/form-data" method="post">
????<input type="file" name="file" />
????<input type="submit" name="upload" />
??</form>
</body>
</html>
MVC 提供了一個 HttpPostedFileBaseModelBinder 將 Request.Files 的信息直接映射給 Action 同名參數。
{
??public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
??{
????...
????HttpPostedFileBase theFile = controllerContext.HttpContext.Request.Files[bindingContext.ModelName];
????// case 1: there was no <input type="file" ... /> element in the post
????if (theFile == null)
????{
??????return null;
????}
????// case 2: there was an <input type="file" ... /> element in the post, but it was left blank
????if (theFile.ContentLength == 0 && String.IsNullOrEmpty(theFile.FileName))
????{
??????return null;
????}
????// case 3: the file was posted
????return theFile;
??}
}
看看一次上傳多個文件的演示。
{
??public ActionResult Index()
??{
????return View();
??}
??public ActionResult Upload(HttpPostedFileBase file1, HttpPostedFileBase file2)
??{
????var html = String.Format("{0}:{1}<br />{2}:{3}",
??????file1.FileName, file1.InputStream.Length,
??????file2.FileName, file2.InputStream.Length);
????return Content(html);
??}
}
Index.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
??<title>Index</title>
</head>
<body>
??<form action="/Test/Upload" enctype="multipart/form-data" method="post">
????<input type="file" name="file1" />
????<input type="file" name="file2" />
????<input type="submit" name="upload" />
??</form>
</body>
</html>
?
我們上邊所看到的Action都是return View();我們可以看作這個返回值用于解析一個aspx文件。而它的返回類型是ActionResult如
public ActionResult Index(){return View();}除了View()之外那我們這里還能用于返回什么值呢?
一、ascx頁面
場景:要返回代碼片斷,比如Ajax返回一個子頁
我們先新建一個Action
public ActionResult Ascx(){return PartialView();}我們下面再建一個View,仍然是在Action中點右鍵,AddView。
注意圖中勾選。
于是新建了一個ascx頁,我們將之少做改寫一下
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %><div> 得到一個DIV </div>運行,得到頁面
?
二、返回文本
除了上述情況,有時我們還會僅返回一段文本。
此時我們可以使用以下Action形式:
public ActionResult Text(){return Content("這是一段文本");}三、返回Json
有時我們在調用Ajax時還會要求返回對象為Json序列化的結果,如:
public ActionResult ShowJson(){var m = new EiceIndexModel{Name = "鄒健",Sex = true};return Json(m);}返回文本:
{"Name":"鄒健","Sex":true}四、輸出JS文件
大多時候js文件都是靜態的,但有時js文件可能也要動態生成這時我們可以這樣輸出
public ActionResult Js(){return JavaScript("var x=0;");}我們訪問之,得到一個正常頁面但其Content-Type:application/x-javascript; charset=utf-8
五、頁面跳轉
1.跳轉到Url
public ActionResult rdurl(){return Redirect("http://www.baidu.com");}2.跳轉到Action
public ActionResult rdaction(){return RedirectToAction("Index","Eice");}3.跳轉到Routing規則
public ActionResult rdrouting(){return RedirectToRoute("Default",//Route名new{Controller = "Eice",Action = "Index"});}六、顯示文件
public ActionResult fn(){return File("/Content/site.css"//文件路徑, "text/css"//文件類型);}?
轉載于:https://www.cnblogs.com/xumingming/archive/2009/09/22/1571510.html
總結
以上是生活随笔為你收集整理的MVC Controller与ActionResult的返回值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java数组转list(Arrays .
- 下一篇: SQL Server 代码颜色