• 理解ASP.NET MVC中的ActionResult [转 深山老林]


    http://www.cnblogs.com/wlb/archive/2009/12/10/1620790.html

    通常我们在一个ASP.NET MVC项目中创建一个Controller的时候,Index()方法默认的返回类型都是ActionResult,通过查看UML图,ActionResult实际上是一个抽象类,因此实际返回的类型是该抽象类的子类

    Ø ActionResult及其子类的对照表

    ActionResult的子类名称

    说明

    ViewResult

    表示HTML的页面内容

    EmptyResult

    表示空白的页面内容

    RedirectResult

    表示定位到另外一个URL

    JsonResult

    表示可以运用到AJAX程序中JSON结果

    JavaScriptResult

    表示一个JavaScript对象

    ContentResult

    表示一个文本内容

    FileContentResult

    表示一个可以下载的、二进制内容的文件

    FilePathResult

    表示一个可以下载的、指定路径的文件

    FileStreamResult

    表示一个可以下载的、流式的文件

    通过我们在代码中编写,不难看到如下的一段代码:

    public ActionResult Index()

            {

                return View();

            }

    可能有人会有疑问,既然我定义的是ActionResult,为什么返回值会是View呢?

    其实这个View的类型是ActionResult的子类ViewResult,有关Controller中的方法与返回对象请参照下表:

    Controller中的方法

    返回对象

    View

    ViewResult

    Redirect

    RedirectResult

    RedirectToAction

    RedirectToActionResult

    RedirectToRoute

    RedirectToRouteResult

    Json

    JsonResult

    JavaScriptResult

    JavaScriptResult

    Content

    ContentResult

    File

    FileContentResultFilePathResultFileStreamResult

    Untitled Page
    微软北京.NET俱乐部13号群:249636398 作者:深山老林 出处:http://wlb.cnblogs.com/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

    例子:

         public ActionResult query_cookie_info()
            {
                bool loginStatus = false;
                string _nk_ = string.Empty;
                if (isAuthenticated())
                {
                    loginStatus = true;
                    SystemUser systemUser = this.iUserService.GetOrCreateUser(this.HttpContext);
                    _nk_  = getGracefulName(systemUser);
                }
                string script = "var userCookie={_nk_:'" +  _nk_ + "',_l_g_:'" + (loginStatus?"1":"") +"',uc1:'',mt:'',l:'',version:''};TB && TB.Global && TB.Global.run && TB.Global.run();";
                return JavaScript(script);
            }

     例子:

     public ActionResult login(LoginModel loginModel,string tpl_redirect_url,string style)
            {
                bool remember=false;
                string userID = string.Empty;
                 if (vr.isSucceed)
                {                
                    userID = loginUser.SystemUser.Id;
                    populateFormAuthCookie(remember, userID, "");
                    if (false /*is full login*/)
                    {
                        return RedirectToAction("MyCart", new { controller = "Home", area = "Cart" });
                    }
                    return Redirect(tpl_redirect_url);
                }
    。。。。。

    例子:

        [Description("显示相应的类别对应的新闻")]
            public ActionResult GetNewsBySectionID(string sectionID, int? pageIndex, int? pageSize, string viewName)
            {
                int _pageSize = pageSize ?? 10;
                int _pageIndex = pageIndex ?? 0;
                string _viewName = viewName ?? "LiWithEndDateWidget";
                IList<DataResource> listnews = _newsRepository.GetNewsBySectionID(sectionID, _pageIndex, _pageSize);
                return View(_viewName, listnews);
            }

    例子:

        [Description("点击dtree时根据点击的节点转到不同的页面")]
            public ActionResult TreeResearchView(string flag)
            {
                string MetaSetID = flag;
                return RedirectToAction("TreeResearch", "MetaObjectService", new { MSID = MetaSetID });
            }

    RedirectToAction方式从一个action跳转到另一个action

     

    重定向到UserController.cs中的Edit()函数,如果不在同一个Controller中,那么雷同重定向到HomeController.cs中的Index()函数

  • 相关阅读:
    页面中多个小图片元素合成一个大图片之后用CSS调用
    腾讯设计中心博客
    php 配置 curl , gd , openssl , mbstring
    Apache开启Rewrite环境
    防止入侵:My SQL各种攻击方法大全
    Css背景图合并工具功能增强
    php防CC攻击代码
    网站地址栏的图标代码
    PHP漏洞全解(一)PHP网页的安全性问题
    用PHP实现飞信api接口发飞信短信
  • 原文地址:https://www.cnblogs.com/wukong0214/p/2953467.html
Copyright © 2020-2023  润新知