首先咱们了解一下对action的要求:javascript
1.必须是一个public方法html
2.必须是实例方法java
3.不能被重载web
4.必须返回ActionResult类型json
表示一个视图结果,它根据视图模板产生应答内容。对应的Controller方法为View。服务器
表示一个部分视图结果,与ViewResult本质上一致,只是部分视图不支持母版,对应于ASP.NET,ViewResult至关于一个Page,而PartialViewResult 则至关于一个UserControl。它对应Controller方法的PartialView.app
表示一个链接跳转,至关于ASP.NET中的Response.Redirect方法,对应得Controller方法为Redirect。编码
一样表示一个跳转,MVC会根据咱们指定的路由名称或路由信息(RouteValueDictionary)来生成Url地址,而后调用Response.Redirect跳转。对应的Controller方法为RedirectToAction和RedirectToRoute.spa
返回简单的纯文本内容,可经过ContentType属性指定应答文档类型,经过ContentEncoding属性指定应答文档的字符编码。可经过Controller类中的Content方法便捷地返回ContentResult对象。若是控制器方法返回非ActionResult对象,MVC将简单地以返回对象的toString()内容为基础产生一个ContentResult对象。code
返回一个空的结果,若是控制器方法返回一个null ,MVC将其转换成EmptyResult对象。
本质上是一个文本内容,只是将Response.ContentType设置为application/x-javascript,此结果应该和MicrosoftMvcAjax.js脚本配合使用,客户端接收到Ajax应答后,将判断Response.ContentType的值,若是是application/x-javascript,则直接eval 执行返回的应答内容,此结果类型对应得Controller方法为JavaScript.
表示一个Json结果。MVC将Response.ContentType 设置为application/json,并经过JavaScriptSerializer类指定对象序列化为Json表示方式。须要注意,默认状况下,Mvc不容许GET请求返回Json结果,要解除此限制,在生成JsonResult对象时,将其JsonRequestBehavior属性设置为JsonRequestBehavior.AllowGet,此结果对应Controller方法的Json.
这三个类继承于FileResult,表示一个文件内容,三者区别在于,FilePath 经过路径传送文件到客户端,FileContent 经过二进制数据的方式,而FileStream 是经过Stream(流)的方式来传送。Controller为这三个文件结果类型提供了一个名为File的重载方法。
FilePathResult: 直接将一个文件发送给客户端
FileContentResult: 返回byte字节给客户端(好比图片)
FileStreamResult: 返回流
表示一个未经受权访问的错误,MVC会向客户端发送一个401的应答状态。若是在web.config 中开启了表单验证(authenication mode=”Forms”),则401状态会将Url 转向指定的loginUrl 连接。
返回一个服务器的错误信息
返回一个找不到Action错误信息
部分代码以下:
public ActionResult ContentDemo() { string str = "content"; return Content(str); } public ActionResult FileDemo1() { FileStream fs = new FileStream(Server.MapPath(@"/File/001.png"), FileMode.Open, FileAccess.Read); byte[] buffer = new byte[Convert.ToInt32(fs.Length)]; fs.Read(buffer, 0, buffer.Length); string fileType = "image/png"; return File(buffer, fileType); } public ActionResult fileDemo2() { string path = Server.MapPath(@"/File/001.png"); string fileType = "image/png"; return File(path, fileType); } public ActionResult fileDemo3() { FileStream fs = new FileStream(Server.MapPath(@"/File/001.png"), FileMode.Open, FileAccess.Read); string fileType = "image/png"; return File(fs, fileType); } public ActionResult httpStatusCodeDemo() { return new HttpStatusCodeResult(500, "System error"); } public ActionResult javaScriptDemo() { return JavaScript(@"<Script>alert('JavaScriptDemo');</Script>") } public ActionResult jsonDemo() { return Json("test"); }