•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");
}

Controller 中Action 返回值类型 及其 页面跳转的用法的更多相关文章

  1. Web API中的返回值类型

    WebApi中的返回值类型大致可分为四种: Void/ IHttpActionResult/ HttpResponseMessage /自定义类型 一.Void void申明方法没有返回值,执行成功后 ...

  2. ASP.NET Core MVC中的IActionFilter.OnActionExecuted方法执行时,Controller中Action返回的对象是否已经输出到Http Response中

    我们在ASP.NET Core MVC项目中有如下HomeController: using Microsoft.AspNetCore.Mvc; namespace AspNetCoreActionF ...

  3. Controller中方法返回值其他类型需要添加jackson依赖

    第一个 第二个: 第三个 https://www.cnblogs.com/codejackanapes/p/5569013.html:json的博客园 springmvc默认的是:2.Jackson: ...

  4. MyBatis中Mapper的返回值类型

    insert.update.delete语句的返回值类型 对数据库执行修改操作时,数据库会返回受影响的行数. 在MyBatis(使用版本3.4.6,早期版本不支持)中insert.update.del ...

  5. ResultMap和ResultType在使用中的区别、MyBatis中Mapper的返回值类型

    在使用mybatis进行数据库连接操作时对于SQL语句返回结果的处理通常有两种方式,一种就是resultType另一种就是resultMap,下面说下我对这两者的认识和理解 resultType:当使 ...

  6. springMVC入门(四)------参数绑定与返回值类型

    简介 从之前的介绍,已经可以使用springMVC完成完整的请求.返回数据的功能. 待解决的问题:如何将数据传入springMVC的控制器进行后续的处理,完成在原生servlet/jsp开发中Http ...

  7. Asp.net MVC 中Controller返回值类型ActionResult

    [Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...

  8. SpringMVC中Controller的返回值类型

    Controller方法的返回值可以有以下几种: 1.返回ModelAndView 返回ModelAndView时最常见的一种返回结果.需要在方法结束的时候定义一个ModelAndView对象,并对M ...

  9. ASP.NET Core中的Action的返回值类型

    在Asp.net Core之前所有的Action返回值都是ActionResult,Json(),File()等方法返回的都是ActionResult的子类.并且Core把MVC跟WebApi合并之后 ...

随机推荐

  1. struct2(二) struct2的hello world 程序

    在struct2 的web应用程序中,当你点击一个超链接或者提交一个HTML页面的时候,并不是直接的转向一个另一个的页面,而是转到你提供的一个Java 类.这个过程被称为一个action,一个acti ...

  2. cf445A DZY Loves Chessboard

    A. DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input stand ...

  3. <php>PDO用法一

    <?php //造PDO对象 $pdo = new PDO("mysql:dbname=mydb;host=localhost","root"," ...

  4. Citrix 服务器虚拟化之九 Xenserver虚拟机的XenMotion

    Citrix 服务器虚拟化之九 Xenserver虚拟机的XenMotion XenMotion 是 XenServer 的一项功能,能够将正在运行的虚拟机从一台 XenServer 主机上迁移到另外 ...

  5. Linux多任务编程——线程

    线程基础 △ 由于进程的地址空间是私有的,因此在进行上下文切换时,系统开销比较大 △ 在同一个进程中创建的线程共享该进程的地址空间 △ 通常线程值得是共享相同地址空间的多个任务 △ 每个线程的私有这些 ...

  6. redis主从 哨兵

    entinel是redis高可用的解决方案,sentinel系统(N个sentinel实例,N >= 1)可以监视一个或者多个redis master服务,以及这些master服务的所有从服务: ...

  7. Ubantu下编译Linux Kernel

    wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.9.3.tar.gztar -xzf linux-3.9.3.tar.gzcd li ...

  8. xhtml规范

    在使用XHTML语言进行网页制作时,必须要遵循一定的语法规范.下面进行详细讲解,其中具体内容可以分为以下几点. 文档方面: 必须定义文档类型(DTD)和你的名字空间 标签方面: 所有标签均要小写,合理 ...

  9. (转)[老老实实学WCF] 第二篇 配置WCF

    在上一篇中,我们在一个控制台应用程序中编写了一个简单的WCF服务并承载了它.先回顾一下服务端的代码: using System; using System.Collections.Generic; u ...

  10. 模型 Model

    模型层包含所有视图或控制器不包含的应用程序逻辑 模型应该包含所有应用程序业务逻辑和数据库访问逻辑 主要部分 bll和dal 例如,使用ado.net或者ef5.0访问sql数据库的代码