一、所有的Controller都继承自System.Web.Mvc.Controller

  目前ASP.NET MVC3默认提供了多种ActionResult的实现,在System.Web.Mvc命名空间里。

  其中ActionResult是一个抽象类,所有一下的Result都继承自它,因此如果一个Action的返回值是ActionResult的话,可以返回以下任意一种类型的值,但是如果限制死了返回值为以下任意一种Result,则只能够返回指定的类型的数据了。

  • ContentResult
  • EmptyResult
  • FileResult
  • HttpStatusCodeResult
  • HttpNotFoundResult
  • HttpUnauthorizedResult
  • JavaScriptResult
  • JsonResult
  • RedirectResult
  • RedirectToRouteResult
  • ViewResultBase
  • PartialViewResult
  • ViewResult
        public ContentResult Index()
{
return Content("测试"); //浏览器显示测试
} public EmptyResult Index()
{
return new EmptyResult(); //浏览器显示空白
} public FileResult Index()
{
return File(Server.MapPath("~/demo.jpg"), "application/x-jpg", "demo.jpg"); //浏览器直接下载demo.jpg
} public HttpNotFoundResult Index()
{
return HttpNotFound(); //报404错误
} public HttpUnauthorizedResult Index()
{
return new HttpUnauthorizedResult(); //未授权的页面,跳转到/Account/LogOn
} public JavaScriptResult hello()
{
string js = "alert('你还好吗?');";
return JavaScript(js); //页面显示 alert('你还好吗?');} 并不会执行这个js,要执行这个js可以在任意视图里<script src="@Url.Action("hello")" type="text/javascript"></script>
} public JsonResult Index()
{
var jsonObj = new
{
Id = 1,
Name = "小铭",
Sex = "男",
Like = "足球"
}; return Json(jsonObj, JsonRequestBehavior.AllowGet); //返回一个JSON,可以将此代码输出到JS处理展示
} public RedirectResult Index()
{
return Redirect("~/demo.jpg"); //可以跳转到任意一个路径
return Redirect("http://www.baidu.com");
return Redirect("/list");
} public RedirectToRouteResult Index()
{
return RedirectToRoute( //跳转到指定Action
new
{
controller = "Home",
action = "GetName"
});
} public ViewResult Index()
{
return View(); //这个是最常用的,返回指定视图
//return View("List");
//return View("/User/List");
} public PartialViewResult Index()
{
return PartialView(); //部分视图,可以作为一个部分引入另外一个视图中,跟View大致相同
}

MVC之ActionResult的更多相关文章

  1. .NET MVC之ActionResult

    .NET MVC之ActionResult ActionResult是所有Controler返回值的父类.各种结果都是由ActionResult包装后发往客户端的. 继承结构 System.Objec ...

  2. springboot中扩展ModelAndView实现net mvc的ActionResult效果

    最近在写spring boot项目,写起来感觉有点繁琐,为了简化spring boot中的Controller开发,对ModelAndView进行简单的扩展,实现net mvc中ActionResul ...

  3. ASP.NET MVC自定义ActionResult实现文件压缩

    有时候需要将单个或多个文件进行压缩打包后在进行下载,这里我自定义了一个ActionResult,方便进行文件下载 using System; using System.Collections; usi ...

  4. C# MVC 自定义ActionResult实现EXCEL下载

    前言 在WEB中,经常要使用到将数据转换成EXCEL,并进行下载.这里整理资料并封装了一个自定义ActionResult类,便于使用.如果文章对你有帮助,请点个赞. 话不多少,这里转换EXCEL使用的 ...

  5. ASP.NET MVC 拓展ActionResult实现Html To Pdf 导出

    之前实现了html直接转换为word文档的功能,那么是否也同样可以直接转换为pdf文档呢,网上搜了下html to pdf 的开源插件有很多 如:wkhtmltopdf,pdfsharp,itexts ...

  6. [转载]深入理解ASP.NET MVC之ActionResult

    Action全局观 在上一篇最后,我们进行到了Action调用的“门口”: 1 if (!ActionInvoker.InvokeAction(ControllerContext, actionNam ...

  7. Asp.net MVC 之 ActionResult

    Action运行完后,回传的值通过ActionResult 类别或者其衍生的类别操作.ActionResult是一个抽象类,因此,Asp.net MVC 本身就实作了许多不同类型的ActionResu ...

  8. asp.net mvc之ActionResult

    Web服务器接收到一个客户端请求以后,会对请求予以相应,而这个响应是通过Response来控制的, 但是在asp.net mvc 里,这部分的工作是由ActionResult来完成的, ActionR ...

  9. ASP.NET MVC 中 ActionResult 和 ViewResult 在使用上的区别

    如果确认你返回的是一个视图(view),你可以直接返回类型为ViewResult. 如果你并不是很清楚,或者你根本不想去理解这些东西,你可以直接返回ActionResult

随机推荐

  1. sign starfieldtech

    signtool sign /f certfile.pfx /p password /tr http://tsa.starfieldtech.com /td SHA256 mycode.exe htt ...

  2. 字符串转换为float<1>

    zjtest7-frontend:/usr/local/logstash-2.3.4/config# cat g01.conf input {stdin{}} filter { grok { matc ...

  3. 精通find命令

    一.前言 find命令是linux使用过程中经常用到的命令,但可能大家只会如下使用find find ./ 或者这样使用 find ./ | grep str 上述命令等同于 find ./ -nam ...

  4. WifiDog系统

    WifiDog:A captive portal suite What is it composed of ? A: It is composed of 2 components: The clien ...

  5. Java应用开发的一条经验

    一旦为应用建立良好的基础设施, 后续的开发就会变得容易而快速.  这些基础设施包括: 1.   线程池的建立.配置: 在 JDK 并发库的基础上建立更适合于应用的并发使用接口: 2.   跨多数据源的 ...

  6. vi命令笔记

    vim编辑器 文本编辑器,字处理器ASCII nano, sed vi: Visual Interfacevim: VI iMproved 全屏编辑器,模式化编辑器 vim模式:编辑模式(命令模式)输 ...

  7. jsp页面判断文件上传类型

    <script language="javascript" type="text/javascript"> function check_file( ...

  8. TCP三次握手的过程

    三次握手 下图就是wireshark抓包工具抓获的TCP连接建立的三次握手过程: http://www.cnblogs.com/hnrainll/archive/2011/10/14/2212415. ...

  9. BFC,IFC,GFC,FFC的定义及功能

    What's FC?一定不是KFC,FC的全称是:Formatting Contexts,是W3C CSS2.1规范中的一个概念.它是页面中的一块渲染区域,并且有一套渲染规则,它决定了其子元素将如何定 ...

  10. long类型在C#和C++中的异同

    C++中long是32位的整数类型.   而在C#中long是64位的,对应包装类型是Int64,int对应Int32.   显然C++中的long类型,而应该对应C#中的int,   C#调用C++ ...