所看到的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"//文件类型
); 
}

MVC Controller return 格式的更多相关文章

  1. MVC Controller return 格式分类及用法

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

  2. MVC Controller return 格式之JsonResult、ContentResult、RedirectResult……

      //语法 public class JsonResult : ActionResult public class ContentResult : ActionResult public class ...

  3. mvc Controller类介绍

    1.Controller类 i.Controller必须为公开类: ii.必须以Controller结尾: iii.继承Controller基类或实现IController接口的类: iv.类中必须包 ...

  4. .NET/ASP.NET MVC Controller 控制器(IController控制器的创建过程)

    阅读目录: 1.开篇介绍 2.ASP.NETMVC IControllerFactory 控制器工厂接口 3.ASP.NETMVC DefaultControllerFactory 默认控制器工厂 4 ...

  5. MVC Controller Dependency Injection for Beginners【翻译】

    在codeproject看到一篇文章,群里的一个朋友要帮忙我翻译一下顺便贴出来,这篇文章适合新手,也算是对MEF的一个简单用法的介绍. Introduction In a simple stateme ...

  6. 转:【Spring MVC Controller单例陷阱】

    http://lavasoft.blog.51cto.com/62575/1394669/ Spring MVC Controller默认是单例的: 单例的原因有二:1.为了性能.2.不需要多例. 1 ...

  7. MVC Controller 链接到 API Controller 以及反向链接

    MVC Controller 链接到 API Controller 以及反向链接 问题 想创建一个从 ASP.NET MVC controller 到 ASP.NET Web API controll ...

  8. Spring MVC Controller单例陷阱

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://lavasoft.blog.51cto.com/62575/1394669 Spr ...

  9. .net Mvc Controller 接收 Json/post方式 数组 字典 类型 复杂对象

    原文地址:http://www.cnblogs.com/fannyatg/archive/2012/04/16/2451611.html ------------------------------- ...

随机推荐

  1. a href="#"与a href="####"的区别是什么

    前提是 :有滚动条. [感谢  黎明就在眼前 博客园园友] '#' 是有特殊意义,如果 '#' 后有内容会被认为是一个标签而从页面找到相应标签跳转到该处,找不到时会跳到页首,通常情况下使用“#ID”来 ...

  2. 传感器- 加速计 - CoreMotion

    /** *  CoreMotion * */ #import "ViewController.h" #import <CoreMotion/CoreMotion.h> ...

  3. 解决Android SDK Manager更新(一个更新Host的程序的原理实现和源码)

    <ignore_js_op>     同学遇到了更新Android SDK的问题,而且Goagent现在也无法用来更新.就想到了用替代Host的方法,添加可用的谷歌地址来实现更新.    ...

  4. nump中的为随机数产生器的seed

    在python的程序中,发现了如下的伪随机数产生的代码 rng = numpy.random.RandomState(23355) arrayA = rng.uniform(0,1,(2,3)) 该段 ...

  5. hairline!ios实现边框0.5px

    在2014WWDC上,Ted O’Connor提出了“retina hairlines”的解决方案,即在ratina屏幕上可以显示0.5px宽度的边框.他的方案是这样的: 1 Standard bor ...

  6. 工作总结:文件对话框的分类(C++)

    原文地址:http://www.jizhuomi.com/software/173.html 文件对话框分为打开文件对话框和保存文件对话框,相信大家在Windows系统中经常见到这两种文件对话框.例如 ...

  7. structDemo1

    structDemo1 # include <iostream.h> # include <malloc.h> enum EType{ One = ,Tow,Three }; ...

  8. 对SVM的个人理解

    对SVM的个人理解 之前以为SVM很强大很神秘,自己了解了之后发现原理并不难,不过,“大师的功力在于将idea使用数学定义它,使用物理描述它”,这一点在看SVM的数学部分的时候已经深刻的体会到了,最小 ...

  9. 【POJ 2152】 Fire (树形DP)

    Fire   Description Country Z has N cities, which are numbered from 1 to N. Cities are connected by h ...

  10. Java+JQuery实现网页显示本地文件目录(含源码)

    原文地址:http://www.cnblogs.com/liaoyu/p/uudisk.html 源码地址:https://github.com/liaoyu/uudisk 前段时间为是练习JQuer ...