MVC所有的ActionResult
2018-06-22 06:20:48来源:未知 阅读 ()
一、所有的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大致相同
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:常用的算法时间复杂度和空间复杂度
- Asp.net MVC SignalR来做实时Web聊天实例代码 2020-03-29
- ASP.NET MVC中jQuery与angularjs混合应用传参并绑定数据 2020-03-29
- ASP.NET MVC Admin主页快速构建 2020-03-23
- Asp.Net MVC4通过id更新表单内容的思路详解 2020-03-19
- MVC数据验证详解 2020-03-14
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
