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

原文地址:https://www.cnblogs.com/niaowo/p/3777815.html

•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 list = new List();
list.Add("2");
list.Add("3");
return Json(list,JsonRequestBehavior.AllowGet);
}

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

  JavaScriptResult -返回 JavaScriptResult.

public ActionResult Test3()
{

return JavaScript("");

}

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段可以调用GetTime Action返回的代码段。

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

  Content -返回 ContentResult action result.

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

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

  File -返回 FileContentResult, FilePathResult, or FileStreamResult .

/// 
        /// http://localhost:1847/Demo/FileContentResultDemo
        /// 
        /// 
        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");
        }

        /// 
        /// http://localhost:1847/Demo/FilePathResultDemo
        /// 
        /// 
        public ActionResult FilePathResultDemo()
        {
            //可以将一个jpg格式的图像输出为gif格式
            return File(Server.MapPath(@"/resource/Images/2.jpg"), @"image/gif");
        }

        /// 
        /// http://localhost:1847/Demo/FileStreamResultDemo
        /// 
        /// 
        public ActionResult FileStreamResultDemo()
        {            
            FileStream fs = new FileStream(Server.MapPath(@"/resource/Images/1.gif"), FileMode.Open, FileAccess.Read);
            return File(fs, @"image/gif");
        }

 

你可能感兴趣的:(asp.net,MVC)