分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

MVC所有的ActionResult

发布时间:2023-09-06 02:00责任编辑:白小东关键词:MVC

一、所有的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

原文地址:https://www.cnblogs.com/DSC1991/p/9205334.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved