MVC之ActionResult

一、所有的Controller都继承自System.Web.Mvc.Controller

  目前ASP.NET MVC3默认提供了多种ActionResult的实现,在System.Web.Mvc命名空间里。

  其中ActionResult是一个抽象类,所有一下的Result都继承自它,因此如果一个Action的返回值是ActionResult的话,可以返回以下任意一种类型的值,但是如果限制死了返回值为以下任意一种Result,则只能够返回指定的类型的数据了。

  • ContentResult
  • EmptyResult
  • FileResult
  • HttpStatusCodeResult
  • HttpNotFoundResult
  • HttpUnauthorizedResult
  • JavaScriptResult
  • JsonResult
  • RedirectResult
  • RedirectToRouteResult
  • ViewResultBase
  • PartialViewResult
  • ViewResult
复制代码
复制代码
        public ContentResult Index()
        {
            return Content("测试");       //浏览器显示测试
        }

        public EmptyResult Index()
        {
            return new EmptyResult();     //浏览器显示空白            
        }

        public FileResult Index()
        {
            return File(Server.MapPath("~/demo.jpg"), "application/x-jpg", "demo.jpg");        //浏览器直接下载demo.jpg   
        }

        public HttpNotFoundResult Index()
        {
            return HttpNotFound();     //报404错误          
        }

        public HttpUnauthorizedResult Index()
        {
            return new HttpUnauthorizedResult();     //未授权的页面,跳转到/Account/LogOn          
        }

        public JavaScriptResult hello()
        {
            string js = "alert('你还好吗?');";
            return JavaScript(js);      //页面显示 alert('你还好吗?');} 并不会执行这个js,要执行这个js可以在任意视图里<script src="@Url.Action("hello")" type="text/javascript"></script>     
        }

        public JsonResult Index()
        {
            var jsonObj = new
            {
                Id = 1,
                Name = "小铭",
                Sex = "",
                Like = "足球"
            };

            return Json(jsonObj, JsonRequestBehavior.AllowGet);     //返回一个JSON,可以将此代码输出到JS处理展示
        }

        public RedirectResult Index()
        {
            return Redirect("~/demo.jpg");      //可以跳转到任意一个路径
            return Redirect("http://www.baidu.com");
            return Redirect("/list");
        }

        public RedirectToRouteResult Index()
        {
            return RedirectToRoute(     //跳转到指定Action
            new
            {
                controller = "Home",
                action = "GetName"
            });
        }

        public ViewResult Index()
        {
            return View();          //这个是最常用的,返回指定视图
            //return View("List");
            //return View("/User/List");
        }

        public PartialViewResult Index()
        {
            return PartialView();          //部分视图,可以作为一个部分引入另外一个视图中,跟View大致相同
        }
复制代码

posted on   itjeff  阅读(324)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示