• Controller 中Action 返回值类型 及其 页面跳转的用法


     
     
    •Controller 中Action 返回值类型

      View – 返回  ViewResult,相当于返回一个View 页面.

    ----------------------------------------------------------------------------------------------------

    Redirect -返回  RedirectResult,跳转到特定的URL.

    //return Redirect("/test/index");

    ----------------------------------------------------------------------------------------------------

    RedirectToAction -返回 RedirectToRouteResult ,跳转到其他Action.

    return RedirectToAction("About");

    ----------------------------------------------------------------------------------------------------

    RedirectToRoute -返回 RedirectToRouteResult,跳转到其他URL.

    return RedirectToRoute(new { controller = "Test", action = "index",page = 2,id=2,name=3}); //http://localhost:2587/Test/index/2?page=2&name=3

    RouteValueDictionary rvd = new RouteValueDictionary
    {
    {"controller", "Test"},
    {"action", "index"},
    {"page", 2},
    {"id", 2},
    {"name", 3}
    };
    return RedirectToRoute(rvd);


      //return Redirect("/test/index");

    ----------------------------------------------------------------------------------------------------

    Json -返回 JsonResult.

    public JsonResult Test3()
    {
    List<string> list = new List<string>();
    list.Add("2");
    list.Add("3");
    return Json(list,JsonRequestBehavior.AllowGet);
    }

    ----------------------------------------------------------------------------------------------------

      JavaScriptResult -返回 JavaScriptResult.

    public ActionResult Test3()
    {

    return JavaScript("<script>alert('dd');</script>");

    }

    JavaScriptResult

    查了很多的资料都没有发现JavaScriptResult的实际用法

    个人不建议使用JavaScriptResult的文章,因为这样脚本和后台不分离。

    很多文章是按一下方式来使用JavaScriptResult的:

    Action

    public ActionResult GetTime()
    {
        var script = string.Format("$('#myPnl').html('{0}');", DateTime.Now);
        return JavaScript(script);
    }

    View

    @Html.ActionLink("Click Me", "GetTime")

    这样的执行结果是页面并不会执行这一段script,而是将这段script作为文件Response出来。

    既然执行GetTime这个Action其实取得script这段脚本的文件,那么就可以这样来使用:

    <script src="@Url.Action("GetTime")" type="text/javascript"></script>
    

    这样的结果就是该script段可以调用GetTime Action返回的代码段。

    ----------------------------------------------------------------------------------------------------

      Content -返回 ContentResult action result.

    /// <summary>
            /// http://localhost:1847/Demo/ContentResultDemo
            /// </summary>
            /// <returns></returns>
            public ActionResult ContentResultDemo()
            {
                string contentString = "ContextResultDemo!";
                return Content(contentString);
            }

    ----------------------------------------------------------------------------------------------------

      File -返回 FileContentResult, FilePathResult, or FileStreamResult .

    /// <summary>
            /// http://localhost:1847/Demo/FileContentResultDemo
            /// </summary>
            /// <returns></returns>
            public ActionResult FileContentResultDemo()
            {
                FileStream fs = new FileStream(Server.MapPath(@"/resource/Images/1.gif"), FileMode.Open, FileAccess.Read);
                byte[] buffer = new byte[Convert.ToInt32(fs.Length)];
                fs.Read(buffer, 0, Convert.ToInt32(fs.Length) );
                return File(buffer, @"image/gif");
            }
    
            /// <summary>
            /// http://localhost:1847/Demo/FilePathResultDemo
            /// </summary>
            /// <returns></returns>
            public ActionResult FilePathResultDemo()
            {
                //可以将一个jpg格式的图像输出为gif格式
                return File(Server.MapPath(@"/resource/Images/2.jpg"), @"image/gif");
            }
    
            /// <summary>
            /// http://localhost:1847/Demo/FileStreamResultDemo
            /// </summary>
            /// <returns></returns>
            public ActionResult FileStreamResultDemo()
            {            
                FileStream fs = new FileStream(Server.MapPath(@"/resource/Images/1.gif"), FileMode.Open, FileAccess.Read);
                return File(fs, @"image/gif");
            }
    
  • 相关阅读:
    Cookie存储在哪里
    save the transient instance before flushing错误解决办法
    hibernate中简单的增删改查
    hibernate中get和load的区别
    使用Linux命令修改数据库密码
    配置solrcloud
    如何确定Redis集群中各个节点的主从关系
    解决Eclipse Debug 断点调试的source not found问题
    .NET框架
    ORM框架
  • 原文地址:https://www.cnblogs.com/niaowo/p/3777815.html
Copyright © 2020-2023  润新知