MVC之ActionResult
一、所有的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的更多相关文章
- .NET MVC之ActionResult
.NET MVC之ActionResult ActionResult是所有Controler返回值的父类.各种结果都是由ActionResult包装后发往客户端的. 继承结构 System.Objec ...
- springboot中扩展ModelAndView实现net mvc的ActionResult效果
最近在写spring boot项目,写起来感觉有点繁琐,为了简化spring boot中的Controller开发,对ModelAndView进行简单的扩展,实现net mvc中ActionResul ...
- ASP.NET MVC自定义ActionResult实现文件压缩
有时候需要将单个或多个文件进行压缩打包后在进行下载,这里我自定义了一个ActionResult,方便进行文件下载 using System; using System.Collections; usi ...
- C# MVC 自定义ActionResult实现EXCEL下载
前言 在WEB中,经常要使用到将数据转换成EXCEL,并进行下载.这里整理资料并封装了一个自定义ActionResult类,便于使用.如果文章对你有帮助,请点个赞. 话不多少,这里转换EXCEL使用的 ...
- ASP.NET MVC 拓展ActionResult实现Html To Pdf 导出
之前实现了html直接转换为word文档的功能,那么是否也同样可以直接转换为pdf文档呢,网上搜了下html to pdf 的开源插件有很多 如:wkhtmltopdf,pdfsharp,itexts ...
- [转载]深入理解ASP.NET MVC之ActionResult
Action全局观 在上一篇最后,我们进行到了Action调用的“门口”: 1 if (!ActionInvoker.InvokeAction(ControllerContext, actionNam ...
- Asp.net MVC 之 ActionResult
Action运行完后,回传的值通过ActionResult 类别或者其衍生的类别操作.ActionResult是一个抽象类,因此,Asp.net MVC 本身就实作了许多不同类型的ActionResu ...
- asp.net mvc之ActionResult
Web服务器接收到一个客户端请求以后,会对请求予以相应,而这个响应是通过Response来控制的, 但是在asp.net mvc 里,这部分的工作是由ActionResult来完成的, ActionR ...
- ASP.NET MVC 中 ActionResult 和 ViewResult 在使用上的区别
如果确认你返回的是一个视图(view),你可以直接返回类型为ViewResult. 如果你并不是很清楚,或者你根本不想去理解这些东西,你可以直接返回ActionResult
随机推荐
- DHTMLEdit
DHTMLEdit POP3研究了一会,发现如果要写一个类似FOXMAIL的东西,还需要解决一个编辑的问题.以为邮件是支持HTML编辑的. 网上查了一会发现可以使用WINDOWS自带的控件:DHT ...
- Powershell 快捷键
Powershell的快捷键和cmd,linux中的shell,都比较像. ALT+F7 清除命令的历史记录PgUp PgDn 显示当前会话的第一个命令和最后一个命令Enter 执行当前命令End 将 ...
- mutate 转换
zjtest7-frontend:/usr/local/logstash-2.3.4/config# cat geoip.conf input {stdin {} } filter { geoip { ...
- SQL Interview Question
面试的时候发现会问一些SQL的基本问题,在此总结一下. ProgramInterview/SQL 这个网站上的问题还比较全. 1. Join type INNER JOIN: Returns all ...
- OpensStack instance debug
错误: 创建实例 "RuiyTest23" 失败: 请稍后再试 [错误: Virtual Interface creation failed].
- linux下添加中文输入法
一.安装环境 查看linux版本号 [ztteng@ztteng ~]$ lsb_release -aLSB Version: :core-4.0-ia32:core-4.0-noarch:gr ...
- 《Java程序员面试笔试宝典》之 什么是AOP
AOP(Aspect-Oriented Programming,面向切面编程)是对面向对象开发的一种补充,它允许开发人员在不改变原来模型的基础上动态地修改模型从而满足新的需求.例如,在不改变原来业务逻 ...
- JavaScript面向对象之类的继承
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- hdu 5311 Hidden String(find,substr)
Problem Description Today is the 1st anniversary of BestCoder. Soda, the contest manager, gets a str ...
- JS~重写alter与confirm,让它们变成fancybox风格
插件与系统命令 对于很多JS弹框插件来说,都提供了alter,confirm等功能,如fancybox,Boxy等插件,今天来介绍一下如何将系统的alter和confirm替换成指定插件的alter和 ...