1.ViewResult
返回相应的视图
返回和Index同名的view,默认母版页
public ActionResult Index()
{
return View();
}
2.ContentResult 返回字符串
返回“i do like you ”字符串
public ActionResult GetString()
{
return Content("i do like you");
}
3.RedirectResult 重定向
重定向到baidu.com,是Response.Redirect的封装
public ActionResult Demo()
{
return Redirect("http://www.baidu.com");
}
4.RedirectToRouteResult 根据路由进行重定向
重定向到Index
public ActionResult DemoAction()
{
return RedirectToAction("Index");
}
重定向到Student控制器中的Index
public ActionResult DemoAction2()
{
return RedirectToAction("Index","Student");
}
5.FilePathResult 向客户端输出文件
第一个参数:文件路径。第二个参数:文件类型
有很多重载的。。
basePath是一个静态string路径
public ActionResult GetFile(string name)
{
return File(basePath+@"\"+name,"image/png");
}
将文件保存到设定好的路径下
public ActionResult UploadFile(HttpPostedFileBase file)
{
var filename = DateTime.Now.Ticks + file.FileName;
file.SaveAs(basePath + @"\" + filename);
return Content(filename);
}
6.JsonResult 向客户端返回对象的json序列化后的结果
new一个匿名对象,将其转化成json返回,记得加 JsonRequestBehavior.AllowGet
public ActionResult GetJson()
{
return Json(new { id = 1, name = "zhangxuhui" }, JsonRequestBehavior.AllowGet);
}
7.HttpStatusCodeResult 显示不同的状态码信息
返回错误信息,比如404之类的
public ActionResult GetCode()
{
return new HttpStatusCodeResult(System.Net.HttpStatusCode.InternalServerError);
}
8.PartialViewResult返回部分视图
public PartialViewResult GetPartial()
{
return PartialView();
}