MVC to ActionResult Types
2011-06-30 16:51 smat 阅读(734) 评论(0) 编辑 收藏 举报下面个人见解,如有不正确的地方,还请多多提出意见!
MVC中ActionResult 类型:
类名 | 抽象类 | 父类 | 功能 |
ContentResult | 根据内容的类型和编码,数据内容. | ||
EmptyResult | 空方法. | ||
FileResult | abstract | 写入文件内容,具体的写入方式在派生类中. | |
FileContentResult | FileResult | 通过 文件byte[] 写入文件. | |
FilePathResult | FileResult | 通过 文件路径 写入文件. | |
FileStreamResult | FileResult | 通过 文件Stream 写入文件. | |
HttpUnauthorizedResult | 抛出401错误 | ||
JavaScriptResult | 返回javascript文件 | ||
JsonResult | 返回Json格式的数据 | ||
RedirectResult | 使用Response.Redirect重定向页面 | ||
RedirectToRouteResult | 根据Route规则重定向页面 | ||
ViewResultBase | abstract | 调用IView.Render() | |
PartialViewResult | ViewResultBase | 调用父类ViewResultBase 的ExecuteResult方法. 重写了父类的FindView方法. 寻找用户控件.ascx文件 | |
ViewResult | ViewResultBase | 调用父类ViewResultBase 的ExecuteResult方法. 重写了父类的FindView方法. 寻找页面.aspx文件 |
ActionResult是一个普通的结果类型;
ViewResult 呈现视图,就是显示一个aspx页面,以及用于执行结果的方法 ;
{
return View("Homepage");
//return View("~/path/to/some/view.aspx");
//Or, equivalently: return new ViewResult { ViewName = "Homepage" };
}
RedirectToRouteResult 重定向,执行一个行动,给定路由名;
{
return RedirectToAction("Index");
//return RedirectToRoute("MyNamedRoute", new { customParam = "SomeValue" });
}
RedirectResult 重定向URL地址;
{
return Redirect("www.xxx.com");
//return Redirect("~/Home/Index/Application");
}
ContentResult 返回文本数据格式;
{
var txt = "<div><span style=\"color:Red; font-size:small;\">稍候...加载中...</span></div>";
return Content(txt);
}
FilePathResult 返回一个文件;
{
string filename = @"c:\files\file.pdf";
return File(filename, "application/pdf", "AnnualReport.pdf");
}
{
byte[] data = ... // Generate or fetch the file contents somehow
return File(data, "application/pdf", "AnnualReport.pdf");
}
public FileStreamResult ProxyExampleDotCom()
{
WebClient wc = new WebClient();
Stream stream = wc.OpenRead("http://www.example.com/");
return File(stream, "text/html");
}
JsonResult 返回json格式的数据;
{
var jon =new {
name="aa",
code="123"
};
return Json(jon);
}
JavaScriptResult 返回js事件;
{
return JavaScript("alert('Hello, world!');");
}
EmptyResult 基本上什么也不做,给出一个空的响应;
{
return null;
}
{
/// <summary>
/// http://localhost:1847/Demo/ContentResultDemo
/// </summary>
/// <returns></returns>
public ActionResult ContentResultDemo()
{
string contentString = "ContextResultDemo!";
return Content(contentString);
}
/// <summary>
/// http://localhost:1847/Demo/EmptyResultDemo
/// </summary>
/// <returns></returns>
public ActionResult EmptyResultDemo()
{
return new EmptyResult();
}
/// <summary>
/// http://localhost:1847/Demo/FileContentResultDemo
/// </summary>
/// <returns></returns>
public ActionResult FileContentResultDemo()
{
FileStream fs = new FileStream(Server.MapPath(@"/resource/Images/1.gif"), FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[Convert.ToInt32(fs.Length)];
fs.Read(buffer, 0, Convert.ToInt32(fs.Length) );
return File(buffer, @"image/gif");
}
/// <summary>
/// http://localhost:1847/Demo/FilePathResultDemo
/// </summary>
/// <returns></returns>
public ActionResult FilePathResultDemo()
{
//可以将一个jpg格式的图像输出为gif格式
return File(Server.MapPath(@"/resource/Images/2.jpg"), @"image/gif");
}
/// <summary>
/// http://localhost:1847/Demo/FileStreamResultDemo
/// </summary>
/// <returns></returns>
public ActionResult FileStreamResultDemo()
{
FileStream fs = new FileStream(Server.MapPath(@"/resource/Images/1.gif"), FileMode.Open, FileAccess.Read);
return File(fs, @"image/gif");
}
/// <summary>
/// http://localhost:1847/Demo/HttpUnauthorizedResultDemo
/// </summary>
/// <returns></returns>
public ActionResult HttpUnauthorizedResultDemo()
{
return new HttpUnauthorizedResult();
}
/// <summary>
/// http://localhost:1847/Demo/JavaScriptResultDemo
/// </summary>
/// <returns></returns>
public ActionResult JavaScriptResultDemo()
{
return JavaScript(@"alert(""Test JavaScriptResultDemo!"")");
}
/// <summary>
/// http://localhost:1847/Demo/JsonResultDemo
/// </summary>
/// <returns></returns>
public ActionResult JsonResultDemo()
{
var tempObj = new { Controller = "DemoController", Action = "JsonResultDemo" };
return Json(tempObj);
}
/// <summary>
/// http://localhost:1847/Demo/RedirectResultDemo
/// </summary>
/// <returns></returns>
public ActionResult RedirectResultDemo()
{
return Redirect(@"http://localhost:1847/Demo/ContentResultDemo");
}
/// <summary>
/// http://localhost:1847/Demo/RedirectToRouteResultDemo
/// </summary>
/// <returns></returns>
public ActionResult RedirectToRouteResultDemo()
{
return RedirectToAction(@"FileStreamResultDemo");
}
/// <summary>
/// http://localhost:1847/Demo/PartialViewResultDemo
/// </summary>
/// <returns></returns>
public ActionResult PartialViewResultDemo()
{
return PartialView();
}
/// <summary>
/// http://localhost:1847/Demo/RedirectToRouteResultDemo
/// </summary>
/// <returns></returns>
public ActionResult ViewResultDemo()
{
//如果没有传入View名称, 默认寻找与Action名称相同的View页面.
return View();
}
}