ASP.NET MVC ActionResult一些常用的子类

在这里插入图片描述

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();

        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值