我们上边所看到的Action都是return View();我们可以看作这个返回值用于解析一个aspx文件。而它的返回类型是ActionResult如

      public ActionResult Index()
{
return View();
}

除了View()之外那我们这里还能用于返回什么值呢?

一、ascx页面

场景:要返回代码片断,比如Ajax返回一个子页

我们先新建一个Action

        public ActionResult Ascx()
{
return PartialView();
}

我们下面再建一个View,仍然是在Action中点右键,AddView。

注意图中勾选。

于是新建了一个ascx页,我们将之少做改写一下

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

<div>
得到一个DIV
</div>

运行,得到页面

二、返回文本

除了上述情况,有时我们还会仅返回一段文本。

此时我们可以使用以下Action形式:

        public ActionResult Text(){
return Content("这是一段文本");
}

三、返回Json

有时我们在调用Ajax时还会要求返回对象为Json序列化的结果,如:

        public ActionResult ShowJson()
{
var m = new EiceIndexModel
{
Name = "邹健",
Sex = true
};
return Json(m);
}

返回文本:

{"Name":"邹健","Sex":true}

四、输出JS文件

大多时候js文件都是静态的,但有时js文件可能也要动态生成这时我们可以这样输出

        public ActionResult Js()
{
return JavaScript("var x=0;");
}

我们访问之,得到一个正常页面但其Content-Type:application/x-javascript; charset=utf-8

五、页面跳转

1.跳转到Url

        public ActionResult rdurl()
{
return Redirect("http://www.baidu.com");
}

2.跳转到Action

        public ActionResult rdaction()
{
return RedirectToAction("Index","Eice");
}

3.跳转到Routing规则

        public ActionResult rdrouting()
{
return RedirectToRoute("Default",//Route名
new{
Controller = "Eice",
Action = "Index"
});
}

六、显示文件

        public ActionResult fn()
{
return File(
"/Content/site.css"//文件路径
, "text/css"//文件类型
);
}

本文转自:http://blog.csdn.net/pasic/article/details/7110134

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

  • 必须是一个public方法
  • 必须是实例方法
  • 没有标志NonActionAttribute特性的(NoAction)
  • 不能被重载
  • 必须返回ActionResult类型

如:

  1. publicclass MyController : Controller
  2. {
  3. // 必须返回ActionResult类型
  4. public ActionResult HelloWorld()
  5. {
  6. ViewData["Message"] = "Hello World!";
  7. return View();
  8. }
  9. }
public class MyController : Controller
{
// 必须返回ActionResult类型
public ActionResult HelloWorld()
{
ViewData["Message"] = "Hello World!";
return View();
}
}

下面列举Asp.net MVC中Controller中的ActionResult返回类型 1、返回ViewResult视图结果,将视图呈现给网页

  1. public ActionResult About()
  2. {
  3. return View(); // 参数可以返回model对象
  4. }
    public ActionResult About()
{
return View(); // 参数可以返回model对象
}

2、 返回PartialViewResult部分视图结果,主要用于返回部分视图内容在View/Shared目录下创建ViewUserControl.cshtml部分视图

  1. public ActionResult UserControl()
  2. {
  3. ViewBag.Message = "部分视图";
  4. return PartialView("ViewUserControl");
  5. }
       public ActionResult UserControl()
{
ViewBag.Message = "部分视图";
return PartialView("ViewUserControl");
}

页面调用@ViewBag.Message 将输出“部分视图” 3、 返回ContentResult用户定义的内容类型

  1. public ActionResult Content()
  2. {
  3. return Content("Test Content", "text/html"); // 可以指定文本类型
  4. }
        public ActionResult Content()
{
return Content("Test Content", "text/html"); // 可以指定文本类型
}

页面输出“Test Content”;此类型多用于在ajax操作中需要返回的文本内容 4、 返回JsonResult序列化的Json对象

  1. public ActionResult Json()
  2. {
  3. Dictionary<string, object> dic = new Dictionary<string, object>();
  4. dic.Add("id", 100);
  5. dic.Add("name", "hello");
  6. return Json(dic, JsonRequestBehavior.AllowGet);
  7. }
       public ActionResult Json()
{
Dictionary<string, object> dic = new Dictionary<string, object>();
dic.Add("id", 100);
dic.Add("name", "hello");
return Json(dic, JsonRequestBehavior.AllowGet);
}

主要用于返回json格式对象,可以用ajax操作;注意:需要设置参数,JsonRequestBehavior.AllowGet,否则会提示错误:此请求已被阻止,因为当用在 GET 请求中时,会将敏感信息透漏给第三方网站。若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet。 5、返回JavaScriptResult可在客户端执行的脚本

  1. public ActionResult JavaScript()
  2. {
  3. string str = string.Format("alter('{0}');", "弹出窗口");
  4. return JavaScript(str);
  5. }
        public ActionResult JavaScript()
{
string str = string.Format("alter('{0}');", "弹出窗口");
return JavaScript(str);
}

但这里并不会直接响应弹出窗口,需要用页面进行再一次调用。这个可以方便根据不同逻辑执行不同的js操作 6、返回FileResult要写入响应中的二进制输出,一般可以用作要简单下载的功能

  1. public ActionResult File()
  2. {
  3. string fileName = "~/Content/test.zip"; // 文件名
  4. string downFileName = "文件显示名称.zip"; // 要在下载框显示的文件名
  5. return File(fileName, "application/octet-stream", downFileName);
  6. }
       public ActionResult File()
{
string fileName = "~/Content/test.zip"; // 文件名
string downFileName = "文件显示名称.zip"; // 要在下载框显示的文件名
return File(fileName, "application/octet-stream", downFileName);
}

直接下载test.zip后保存到本地则为"文件显示名称.zip" 7、 返回Null或者Void数据类型的EmptyResult

  1. public ActionResult Empty()
  2. {
  3. returnnull;
  4. }
       public ActionResult Empty()
{
return null;
}

返回NULL 8、重定向方法:Redirect / RedirectToAction / RedirectToRoute
    Redirect:直接转到指定的url地址

  1. public ActionResult Redirect()
  2. {
  3. // 直接返回指定的url地址
  4. return Redirect("http://www.baidu.com");
  5. }
	public ActionResult Redirect()
{
// 直接返回指定的url地址
return Redirect("http://www.baidu.com");
}

RedirectToAction:直接使用 Action Name 进行跳转,也可以加上ControllerName

  1. public ActionResult RedirectResult()
  2. {
  3. return RedirectToAction("Index", "Home", new { id = "100", name = "liu" });
  4. }
        public ActionResult RedirectResult()
{
return RedirectToAction("Index", "Home", new { id = "100", name = "liu" });
}

也可以带上参数RedirectToRoute:指定路由进行跳转

  1. public ActionResult RedirectRouteResult()
  2. {
  3. return RedirectToRoute("Default", new { controller = "Home", action = "Index"});
  4. }
        public ActionResult RedirectRouteResult()
{
return RedirectToRoute("Default", new { controller = "Home", action = "Index"});
}

Default为global.asax.cs中定义的路由名称

ActionResult的其它返回值的更多相关文章

  1. ASP.NET MVC 第五回 ActionResult的其它返回值

    我们上边所看到的Action都是return View();我们可以看作这个返回值用于解析一个aspx文件.而它的返回类型是ActionResult如 public ActionResult Inde ...

  2. ASP.NET MVC ActionResult的其它返回值

    一.ascx页面 场景:要返回代码片断,比如Ajax返回一个子页 我们先新建一个Action public ActionResult Ascx() { return PartialView(); } ...

  3. EF5+MVC4系列(8) ActionResult的返回值

    我们在MVC的代码中,经常会看到这样的一个 代码 可能有人会有疑问,既然我定义的是ActionResult,为什么返回值会是View方法呢? 其实这个View方法的返回值的类型是ActionResul ...

  4. Asp.net mvc中Controller的返回值

    (1)EmptyResult:当用户有误操作或者是图片防盗链的时候,这个EmptyResult就可以派上用场,返回它可以让用户啥也看不到内容,通过访问浏览器端的源代码,发现是一个空内容: public ...

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

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

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

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

  7. 列举mvc ActionResult的返回值

    8.列举ASP.NET MVC ActionResult的返回值有几种类型? 主要有View(视图).PartialView(部分视图).Content(内容).Json(Json字符串).Javas ...

  8. ASP.NET MVC中Controller返回值类型ActionResult

    1.返回ViewResult视图结果,将视图呈现给网页 public class TestController : Controller { //必须存在Controller\Test\Index.c ...

  9. Controller返回值类型ActionResult

    在mvc中所有的controller类都必须使用"Controller"后缀来命名 并且对Action也有一定的要求: 必须是一个public方法 必须是实例方法 没有标志NonA ...

随机推荐

  1. 【Python@Thread】threading模块

    theading模块的Thread类 属性: name 线程名 ident 线程标识符 daemon  布尔值,标示是否为守护线程 方法: __init__(target=None, name=Non ...

  2. 数学之欧拉函数 &几道poj欧拉题

    欧拉函数总结+证明 欧拉函数总结2 POJ 1284 原根 #include<iostream> #include<cstdio> #include<cstring> ...

  3. hdu 5664 Lady CA and the graph(树的点分治+容斥)

    题意: 给你一个有n个点的树,给定根,叫你找第k大的特殊链 .特殊的链的定义:u,v之间的路径,经过题给的根节点. 题解:(来自BC官方题解) 对于求第k大的问题,我们可以通过在外层套一个二分,将其转 ...

  4. poj1741_Tree(树的点分治入门题)

    题目链接:poj1741_Tree 题意: 给你一颗n个节点的树,每条边有一个值,问有多少点对(u,v),满足u->v的最短路径小于k. 题解: 典型的树的分治,板子题. #include< ...

  5. WebApi 跨域问题解决方案:CORS

    注:本文为个人学习摘录,原文地址:http://www.cnblogs.com/landeanfen/p/5177176.html 前言:上篇总结了下WebApi的接口测试工具的使用,这篇接着来看看W ...

  6. STL学习:STL库vector、string、set、map用法

    本文仅介绍了如何使用它们常用的方法. vector 1.可随机访问,可在尾部插入元素:2.内存自动管理:3.头文件#include <vector> 1.创建vector对象 一维: (1 ...

  7. [kuangbin带你飞]专题四 最短路练习 POJ 3268 Silver Cow Party

    题意: 在一个有向图中求n头牛从自己的起点走到x再从x走回来的最远距离 思路一开始是暴力跑dij…… 讲道理不太可能…… 然后就百度了一下 才知道把矩阵转置的话就只需要求两次x的单源最短路…… /* ...

  8. Block使用中的一些要注意的地方

    本文主要是阐述一下Block中如何的使用外部变量以及block本身的内存管理. 先定义一个block变量,作为后续的例子中使用: typedef void(^BlockCC)(void); Block ...

  9. 配置php开发环境

    安装apache 1 loadModule 加载php的模块2 addType 告诉apache凡是php结尾的文件都交给php模块执行3 PHPIniDir 告诉apache php.ini的文件在 ...

  10. winsock编程WSAEventSelect模型

    winsock编程WSAEventSelect模型 WSAEventSelect模型和WSAAsyncSelec模型类似,都是用调用WSAXXXXXSelec函数将socket和事件关联并注册到系统, ...