• (转)ASP.NET MVC3默认提供了11种ActionResult的实现


    在System.Web.Mvc命名空间
    ActionResult
    ContentResult
    EmptyResult
    FileResult
    HttpStatusCodeResult
    HttpNotFoundResult
    HttpUnauthorizedResult
    JavaScriptResult
    JsonResult
    RedirectResult
    RedirectToRouteResult
    ViewResultBase
    PartialViewResult

    ViewResult

    示例代码:

    public class ActionResultController : Controller

        {

            public ActionResult Index()

            {

                return View();

            }

            public ActionResult ContentResult()

            {

                return Content("Hi, 我是ContentResult结果");

            }

            public ActionResult EmptyResult()

            {

                //空结果当然是空白了!

                //至于你信不信, 我反正信了

                return new EmptyResult();

            }

            public ActionResult FileResult()

            {

                var imgPath = Server.MapPath("~/demo.jpg");

                return File(imgPath, "application/x-jpg", "demo.jpg");

            }

            public ActionResult HttpNotFoundResult()

            {

                return HttpNotFound("Page Not Found");

            }

            public ActionResult HttpUnauthorizedResult()

            {

                //未验证时,跳转到Logon

                return new HttpUnauthorizedResult();

            }

            public ActionResult JavaScriptResult()

            {

                string js = "alert(\"Hi, I'm JavaScript.\");";

                return JavaScript(js);

            }

            public ActionResult JsonResult()

            {

                var jsonObj = new

                {

                    Id = 1,

                    Name = "小铭",

                    Sex = "男",

                    Like = "足球"

                };

                return Json(jsonObj, JsonRequestBehavior.AllowGet);

            }

            public ActionResult RedirectResult()

            {

                return Redirect("~/demo.jpg");

            }

            public ActionResult RedirectToRouteResult()

            {

                return RedirectToRoute(new {

                    controller = "Hello", action = ""

                });

            }

            public ActionResult ViewResult()

            {

                return View();

            }

            public ActionResult PartialViewResult()

            {

                return PartialView();

            }

            //禁止直接访问的ChildAction

            [ChildActionOnly]

            public ActionResult ChildAction()

            {

                return PartialView();

            }

            //正确使用ChildAction

            public ActionResult UsingChildAction()

            {

                return View();

            }

        }



    欢迎加入JAVA技术交流QQ群:179945282

    欢迎加入ASP.NET(C#)交流QQ群:17534377


  • 相关阅读:
    jdk8中奖Date转换为String格式的方法
    Java Calendar详解
    Java Calendar使用总结
    得到当前时间的小时数
    Java中 break continue return 的区别
    两个QWidget叠加,可部分代替layout的功能
    Qt5官方demo解析集35——Music Player(使用winextras模块)
    P和P1指向了O和O1两个变量(对象)的地址, 而不是O和O1的内容(对象的实际地址)——充分证明@是取变量(对象)的地址,而不是变量里面的内容,够清楚!
    提高Android和iOS调试编译速度
    Delphi 快速获取文件大小(使用_lopen和FileSeek,此函数可以快速获取文件大小,即使文件已经被其它程序锁定)
  • 原文地址:https://www.cnblogs.com/q149072205/p/2409810.html
Copyright © 2020-2023  润新知