MVC中的Action方法的返回值一般有以下几种:
EmptyResult | 不进行任何操作 |
ContentResult | 将指定内容作为文本输出 |
JsonResult | 输出JSON字符串 |
JavaScriptResult | 输出JavaScript |
RedirecResult、RedirectToRouteResult | 重定向到给定的URL中 |
FileResult(抽象类) FilePathResult、FileContentResult、FileStreamResult | 文件输出 |
ViewResultBase(抽象类) ViewResult、PartialViewResult | 调用视图输出 |
Controller控制器代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
namespace MvcActionReturnType.Controllers
{
public class HomeController : Controller
{
/// <summary>
/// 1、返回一个ViewResult对象
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
return View();
}
/// <summary>
/// 2、返回一个json格式的数据
/// </summary>
/// <returns></returns>
public ActionResult Json()
{
var book = new { BookId = 1, BookName = "MVC框架" };
return Json(book, JsonRequestBehavior.AllowGet);
}
/// <summary>
/// 3、返回JavaScript
/// </summary>
/// <returns></returns>
public ActionResult JavaScript()
{
string js = "<script>alert('Welcome to ASP.NET MVC')</script>";
return JavaScript(js);
}
/// <summary>
/// 4、返回FilePath
/// </summary>
/// <returns></returns>
public ActionResult FilePath()
{
//return File("~/Content/校长 - 带你去旅行.mp3", "audio/mp3");
return new FilePathResult("~/Content/校长 - 带你去旅行.mp3", "audio/mp3");
}
/// <summary>
/// 5、返回FileContent
/// </summary>
/// <returns></returns>
public ActionResult FileContent()
{
string content = "Welcome To ASP.NET MVC";
byte[] contents = System.Text.Encoding.UTF8.GetBytes(content);
return File(contents, "text/plain");
}
/// <summary>
/// 6、返回FileStream
/// </summary>
/// <returns></returns>
public ActionResult FileStream()
{
string content = "Welcome To ASP.NET MVC";
byte[] contents = System.Text.Encoding.UTF8.GetBytes(content);
FileStream fs = new FileStream(Server.MapPath("~/Content/2 开发环境下载安装说明.doc"), FileMode.Open);
return File(fs, "application/msword");
}
/// <summary>
/// 7、返回 ContentResult
/// </summary>
/// <returns></returns>
public ActionResult ContentResult()
{
string content = "<h1>Welcome To ASP.NET MVC</h1>";
return Content(content);
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
视图代码如下:
@{
ViewBag.Title = "Home Page";
}
<div>
1.输出json
<p>
@Html.ActionLink("生成Json", "Json");
</p>
2.输出js
<script src=@Url.RouteUrl(new { action="JavaScript"}) type="text/javascript">
</script>
<p>
@(Url.RouteUrl(new { controller = "home", action = "JavaScript" }));
@Html.ActionLink("生成JavaScript", "JavaScript");
</p>
3.播放音乐
<p>
@Html.ActionLink("播放MP3", "FilePath");
</p>
4.查看文本文件
<p>
@Html.ActionLink("查看文件内容", "FileContent");
</p>
5.链接文件流
<p>
@Html.ActionLink("访问Doc", "FileStream");
</p>
6.输出文本
<p>
@Html.ActionLink("输出文本", "ContentResult");
</p>
</div>
请看下面的两段代码:
public int Sum(int num1, int num2)
{
int sum = num1 + num2;
return sum;
}
public ActionResult Sum2(int num1, int num2)
{
int sum = num1 + num2;
return Content(sum.ToString());
}
有些时候,我们希望把Action定义成一种更自然的形式,比如我们希望Action Sum2做一个求和的计算,传入的参数是整数,计算的结果也是整数,那么可不可以返回一个整数的结果呢?
如上面的两个方法所示,两个方法的效果是等价的,也就是会把整形的数据转换成字符串类型,然后封装成ContentResult类型,这个过程是MVC框架自动帮助我们完成的。
null | EmptyResult |
void | EmptyResult |
对象(ActionResult之外的类型) | ContentResult |
在Action方法中,任何类型的返回值都是可以的,如果他不是ActionResult类型,会封装成ActionResult类型的对象,如上表所示。