Controller返回值类型ActionResult

在mvc中所有的controller类都必须使用"Controller"后缀来命名
并且对Action也有一定的要求:

    • 必须是一个public方法
    • 必须是实例方法
    • 没有标志NonActionAttribute特性的(NoAction)
    • 不能被重载
    • 必须返回ActionResult类型

下面列举Asp.net MVC中Controller中的ActionResult返回类型
1、返回ViewResult视图结果,将视图呈现给网页

public ActionResult About()
 {
    return View(); // 参数可以返回model对象
 }  

2、 返回PartialViewResult部分视图结果,主要用于返回部分视图内容
在View/Shared目录下创建ViewUserControl.cshtml部分视图

public ActionResult UserControl()
 {
     ViewBag.Message = "部分视图";
     return PartialView("ViewUserControl");
 }  

页面调用@ViewBag.Message 将输出“部分视图”

3、 返回ContentResult用户定义的内容类型 

public ActionResult Content()
{
   return Content("Test Content", "text/html"); // 可以指定文本类型
}  

页面输出“Test Content”;
此类型多用于在ajax操作中需要返回的文本内容
4、 返回JsonResult序列化的Json对象

public ActionResult Json()
 {
     Dictionary<string, object> dic = new Dictionary<string, object>();
     dic.Add("id", 100);
     dic.Add("name", "hello");
     return Json(dic, JsonRequestBehavior.AllowGet);
 }  

主要用于返回json格式对象,可以用ajax操作;
注意:需要设置参数,JsonRequestBehavior.AllowGet,
否则会提示错误:此请求已被阻止,因为当用在 GET 请求中时,会将敏感信息透漏给第三方网站。
若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet。

5、返回JavaScriptResult可在客户端执行的脚本


public ActionResult JavaScript()
{
    string str = string.Format("alter(‘{0}‘);", "弹出窗口");
    return JavaScript(str);
}  

但这里并不会直接响应弹出窗口,需要用页面进行再一次调用。
这个可以方便根据不同逻辑执行不同的js操作
6、返回FileResult要写入响应中的二进制输出,一般可以用作要简单下载的功能


public ActionResult File()
 {
     string fileName = "~/Content/test.zip"; // 文件名
     string downFileName = "文件显示名称.zip"; // 要在下载框显示的文件名
     return File(fileName, "application/octet-stream", downFileName);
 }  

直接下载test.zip后保存到本地则为"文件显示名称.zip"
7、 返回Null或者Void数据类型的EmptyResult 

public ActionResult Empty()
 {
     return null;
 }  

返回NULL
8、重定向方法:Redirect / RedirectToAction / RedirectToRoute

Redirect:直接转到指定的url地址


public ActionResult Redirect()
       {
           // 直接返回指定的url地址
           return Redirect("http://www.baidu.com");
       }   

9. RedirectToAction:直接使用 Action Name 进行跳转,也可以加上ControllerName

public ActionResult RedirectResult()
{
    return RedirectToAction("Index", "Home", new { id = "100", name = "liu" });
}  

也可以带上参数
RedirectToRoute:指定路由进行跳转

public ActionResult RedirectRouteResult()
{
    return RedirectToRoute("Default", new { controller = "Home", action = "Index"});
}  

Default为global.asax.cs中定义的路由名称

时间: 2024-08-29 21:57:06

Controller返回值类型ActionResult的相关文章

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

内容转自 http://blog.csdn.net/pasic/article/details/7110134 Asp.net MVC中Controller返回值类型 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必须是一个public方法 必须是实例方法 没有标志NonActionAttribute特性的(NoAction) 不能被重载 必须返回ActionResult类型 如: [csharp] view pl

ASP.NET MVC中Controller返回值类型ActionResult

1.返回ViewResult视图结果,将视图呈现给网页 public class TestController : Controller { //必须存在Controller\Test\Index.cshtml文件 public ActionResult Index() { return View(); } } 2. 返回PartialViewResult部分视图结果,主要用于返回部分视图内容 //在View/Shared目录下创建ViewUserControl.cshtml部分视图 publi

SpringMVC Controller 返回值的可选类型

spring mvc 支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void. ModelAndView @RequestMapping("/hello") public ModelAndView helloWorld() { String message = "Hello World, Spring 3.x!"; return new ModelAndView("hello"

Spring MVC方法的返回值类型

一,String类型作为返回值类型 返回值类型为String时,一般用于返回视图名称 1.当方法返回值为Null时,默认将请求路径当做视图 /jsp/thread/secondRequest.jsp 如果说没有试图解析器,如果返回值为Null携带数据只能用JSON 2.当方法返回一个String的字符串时,当字符串为逻辑视图名时只返回视图,如果要携带数据则使用request,session或者Json 如果要用Model或者ModelMap传递数据,那么Model或者ModelMap绝对是方法入

SpringMVC方法的返回值类型和自动装配

1. void类型作为返回值类型 /** * 如果方法写成了void就跟原来servlet含义是差不多 的 * json */ @RequestMapping("/firstRequest") public void firstRequest(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException { UserInfo info=new UserI

SpringMVC返回值类型及响应数据类型

1.SpringMVC 和 Struts2 的优略分析 共同点: 它们都是表现层框架,都是基于 MVC 模型编写的. 它们的底层都离不开原始 ServletAPI. 它们处理请求的机制都是一个核心控制器. 区别: Spring MVC 的入口是 Servlet, 而 Struts2 是 Filter. Spring MVC 是基于方法设计的,而 Struts2 是基于类,Struts2 每次执行都会创建一个动作类.所以 Spring MVC 会稍微比 Struts2 快些. Spring MVC

oc对象函数什么时候返回值类型使用instancetype

oc中定义对象函数时经常会返回本类类型的对象,此时返回值类型用instancetype或者本类对象*都可以,什么区别呢? 其实主要区别在返回值是不是self并且有继承 如果返回值是self并且作为父类,那么返回值最好写成instancetype 举例说明: 父类的声明 @interface Father : NSObject @property(readwrite,nonatomic,assign)NSInteger item; //元素自增 为了比较自增返回类型定为instancetype -

赋值运算符函数的返回值类型详解

在c++赋值运算符函数的学习中,对于返回值类型的问题,一直非常费解,今天彻底总结一些每种不同返回值类型的结果: 1.当返回值为空时: <span style="font-size:14px;">void hasptr::operator=(const hasptr& s)</span> 这个时候如果只有一个'='(a = b)运算那就没问题,但是如果存在'='(a = b = c)的链式操作时,编译器就会报错 我们看:a = b = c: 程序会先运行

javascript函数参数、返回值类型检查

实现带参数.返回值类型声明的js函数: 类型定义:window.Str = Type.Str = Type.define('STRING', Type.isStr);var Per = Type.define('PERSON', function(p){    return p && p.type === 'person' && p.name;}); 定义函数:var addStr = Str(function(a, b){  return a + b;}, Str, St