• (转)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


  • 相关阅读:
    Webservice实践(七)CXF 与Spring结合+tomcat发布
    slave have equal MySQL Server UUIDs原因及解决
    VM+CentOS+hadoop2.7搭建hadoop完全分布式集群
    Ubuntu下MySQL主从同步配置
    解决Ubuntu14.04下vi编辑器不能使用方向键和退格键问题
    Ubuntu解压缩zip,tar,tar.gz,tar.bz2
    sersync+rsync原理及部署
    mysqldump备份成压缩包
    linux下如何实现mysql数据库每天自动备份定时备份
    ubuntu mysql 数据库备份以及恢复[命令行]
  • 原文地址:https://www.cnblogs.com/q149072205/p/2409810.html
Copyright © 2020-2023  润新知