Asp.net MVC 中Controller返回值类型ActionResult

Asp.net MVC中Controller返回值类型

在mvc中所有的controller类都必须使用"Controller"后缀来命名
并且对Action也有一定的要求:
  • 必须是一个public方法
  • 必须是实例方法
  • 没有标志NonActionAttribute特性的(NoAction)
  • 不能被重载
  • 必须返回ActionResult类型
如:
[csharp]  view plain  copy
  1. public class MyController : Controller  
  2. {  
  3.    // 必须返回ActionResult类型  
  4.     public ActionResult HelloWorld()  
  5.     {  
  6.         ViewData["Message"] = "Hello World!";  
  7.         return View();  
  8.     }  
  9. }  
下面列举Asp.net MVC中Controller中的ActionResult返回类型
1、返回ViewResult视图结果,将视图呈现给网页
[csharp]  view plain  copy
  1. public ActionResult About()  
  2.  {  
  3.     return View(); // 参数可以返回model对象  
  4.  }  
2、 返回PartialViewResult部分视图结果,主要用于返回部分视图内容
在View/Shared目录下创建ViewUserControl.cshtml部分视图
  
[csharp]  view plain  copy
  1. public ActionResult UserControl()  
  2.  {  
  3.      ViewBag.Message = "部分视图";  
  4.      return PartialView("ViewUserControl");  
  5.  }  
       页面调用@ViewBag.Message 将输出“部分视图”
3、 返回ContentResult用户定义的内容类型 
[csharp]  view plain  copy
  1. public ActionResult Content()  
  2. {  
  3.    return Content("Test Content""text/html"); // 可以指定文本类型  
  4. }  
 页面输出“Test Content”;
此类型多用于在ajax操作中需要返回的文本内容
4、 返回JsonResult序列化的Json对象
      
[csharp]  view plain  copy
  1. public ActionResult Json()  
  2.  {  
  3.      Dictionary<stringobject> dic = new Dictionary<stringobject>();  
  4.      dic.Add("id", 100);  
  5.      dic.Add("name""hello");  
  6.      return Json(dic, JsonRequestBehavior.AllowGet);  
  7.  }  
主要用于返回json格式对象,可以用ajax操作;
注意:需要设置参数,JsonRequestBehavior.AllowGet,
否则会提示错误:此请求已被阻止,因为当用在 GET 请求中时,会将敏感信息透漏给第三方网站。
若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet。
5、返回JavaScriptResult可在客户端执行的脚本
[csharp]  view plain  copy
  1. public ActionResult JavaScript()  
  2. {  
  3.     string str = string.Format("alter('{0}');""弹出窗口");  
  4.     return JavaScript(str);  
  5. }  
但这里并不会直接响应弹出窗口,需要用页面进行再一次调用。
这个可以方便根据不同逻辑执行不同的js操作
6、返回FileResult要写入响应中的二进制输出,一般可以用作要简单下载的功能
[csharp]  view plain  copy
  1. public ActionResult File()  
  2.  {  
  3.      string fileName = "~/Content/test.zip"// 文件名  
  4.      string downFileName = "文件显示名称.zip"// 要在下载框显示的文件名  
  5.      return File(fileName, "application/octet-stream", downFileName);  
  6.  }  
直接下载test.zip后保存到本地则为"文件显示名称.zip"
7、 返回Null或者Void数据类型的EmptyResult 
[csharp]  view plain  copy
  1. public ActionResult Empty()  
  2.  {  
  3.      return null;  
  4.  }  
返回NULL
8、重定向方法:Redirect / RedirectToAction / RedirectToRoute

    Redirect:直接转到指定的url地址
[csharp]  view plain  copy
  1. public ActionResult Redirect()  
  2.        {  
  3.            // 直接返回指定的url地址  
  4.            return Redirect("http://www.baidu.com");  
  5.        }   
    RedirectToAction:直接使用 Action Name 进行跳转,也可以加上ControllerName
[csharp]  view plain  copy
  1. public ActionResult RedirectResult()  
  2. {  
  3.     return RedirectToAction("Index""Home"new { id = "100", name = "liu" });  
  4. }  
也可以带上参数
RedirectToRoute:指定路由进行跳转
[csharp]  view plain  copy
  1. public ActionResult RedirectRouteResult()  
  2. {  
  3.     return RedirectToRoute("Default"new { controller = "Home", action = "Index"});  
  4. }  
Default为global.asax.cs中定义的路由名称
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值