一、所有的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. BZOJ 1565 植物大战僵尸

    http://www.lydsy.com/JudgeOnline/problem.php?id=1565 思路:由于植物之间有保护关系:(右边的植物保护左边的植物,植物攻击范围内的植物都被保护了),因 ...

  2. BZOJ 1017 魔兽地图DotR(树形DP)

    题意:有两类装备,高级装备A和基础装备B.现在有m的钱.每种B有一个单价和可以购买的数量上限.每个Ai可以由Ci种其他物品合成,给出Ci种其他物品每种需要的数量.每个装备有一个贡献值.求最大的贡献值. ...

  3. LeetCode_Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  4. log4j日志分模块打印,同时不打印到控制台上

    由于定时刷新程序的启用,导致catalina.out配置文件中打入大量日志,致使程序调试困难.          无法正常查看日志.所以客户要求将性能流量配置日志迁移出catalina.out目录.修 ...

  5. GridView视图

    本文实现如下效果 Test_Grid.java public class Test_Grid extends Activity { private GridView gridview; private ...

  6. hdu 3853 LOOPS(概率 dp 期望)

    Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wants to help ...

  7. WebFrom模拟MVC

    如:  aspx前台     这样写生成页面时不会产生新的html标签,用控件则会产生新的html标签 <h1><%= title %></h1> <p> ...

  8. ios delegate 和 block

    //委托的协议定义 @protocol UpdateDelegate <NSObject> - (void)update; @end @interface Test : NSObject ...

  9. 关于MemoryBarrier

    备注:OSG  OpenThread::Atomic.cpp中MemoryBarrier(); Atomic::operator unsigned() const { #if defined(_OPE ...

  10. debian安装mysql

    http://thirteen-tw.blogspot.com/2008/09/debian-mysql-server.html 安裝MySQL-Server debian:~# apt-get in ...