ASP.NET MVC中Action返回值类型
2013-04-01 15:34
309 查看
Action的返回值类型一共提供了十几种。简单概括一下其中很常用的几种类型。
1、ActionResult
所有的 Result 都派生自 ActionResult抽象类,因此 ActionResult 作为基类提供了最基础的功能,ActionResult 是一个抽象类,其声明如下:
2、EmptyResult
在Action中,若要返回一个空的页面(不常用),则可如下:
3、RedirectResult
RedirectResult用于执行转移。事实上 RedirectResult 最终调用了 Response.Redirect() 进行转移,所以您可以使用RedirectResult跳转到任意的包括当前项目或网络上的Url,对于当前项目的路径,因为使用了UrlHelper.Content() 方法获取目标路径,所以RedirectResult传递的Url同时支持当前项目目录标识符 ~ (即应用程序目录)。
4、RedirectToRouteResult
RedirectToRouteResult对于RedirectResult而言,其作用有所局限,仅能转移到路由(路由匹配的结果最终是一条相对当前项目的Url,例如: /Home/Index ),总的来说与RedirectResult的最终作用是一样的,都是执行转移。RedirectResult较为直接地转移到任意指定的Url,而RedirectToRouteResult则转移到指定的路由(路由匹配所得结果最终也是一个的Url):
5、ContentResult
ContentResult用于将字符串直接向客户端输出。ContentResult的ExecuteResult方法实际是调用Response.Write(
string… ),从输出一个简单的字符串到整个页面,Response.Write()都能胜任,所以ContentResult显得特别强大:
若没有提供任何输出的内容,ContentResult呈现的结果与EmptyResult 是一样的,都是输出最基本的<body>标记内容为空的HTML,若内容不为空,则直接输出这些内容(不再输出其他任何 HTML 代码),例如:
事实上从ContentResult我们可以看到一个ActionResult其实并无特别,从前面几个Result 来看,其实不过是Response.Redirect或Response.Write,此外还可以利用二进制流Response.OutputStream.Write向客户端上载文件……据此我们所以拓展编写更多针对实际意义的Result。例如 XmlResult(文件)、RssResult(跟XmlResult其实是一样的)等等。
1、ActionResult
所有的 Result 都派生自 ActionResult抽象类,因此 ActionResult 作为基类提供了最基础的功能,ActionResult 是一个抽象类,其声明如下:
public abstract class ActionResult { public abstract void ExecuteResult(ControllerContext context); }
2、EmptyResult
示public class EmptyResult : ActionResult { private static readonly EmptyResult _singleton = new EmptyResult(); internal static EmptyResult Instance { get { return _singleton; } } public override void ExecuteResult(ControllerContext context) { } }
在Action中,若要返回一个空的页面(不常用),则可如下:
public ActionResult Index() { return new EmptyResult(); }
3、RedirectResult
public override void ExecuteResult(ControllerContext context) { if (context == null) { throw new ArgumentNullException("context"); } string destinationUrl = UrlHelper.Content(Url, context.HttpContext); context.HttpContext.Response.Redirect(destinationUrl, false /* endResponse */); }
RedirectResult用于执行转移。事实上 RedirectResult 最终调用了 Response.Redirect() 进行转移,所以您可以使用RedirectResult跳转到任意的包括当前项目或网络上的Url,对于当前项目的路径,因为使用了UrlHelper.Content() 方法获取目标路径,所以RedirectResult传递的Url同时支持当前项目目录标识符 ~ (即应用程序目录)。
4、RedirectToRouteResult
RedirectToRouteResult对于RedirectResult而言,其作用有所局限,仅能转移到路由(路由匹配的结果最终是一条相对当前项目的Url,例如: /Home/Index ),总的来说与RedirectResult的最终作用是一样的,都是执行转移。RedirectResult较为直接地转移到任意指定的Url,而RedirectToRouteResult则转移到指定的路由(路由匹配所得结果最终也是一个的Url):
public override void ExecuteResult(ControllerContext context) { if (context == null) { throw new ArgumentNullException("context"); } string destinationUrl = UrlHelper.GenerateUrl(RouteName, null /* actionName */, null /* controllerName */, RouteValues, Routes, context.RequestContext, false /* includeImplicitMvcValues */); if (String.IsNullOrEmpty(destinationUrl)) { throw new InvalidOperationException(MvcResources.ActionRedirectResult_NoRouteMatched); } context.HttpContext.Response.Redirect(destinationUrl, false /* endResponse */); }
5、ContentResult
ContentResult用于将字符串直接向客户端输出。ContentResult的ExecuteResult方法实际是调用Response.Write(
string… ),从输出一个简单的字符串到整个页面,Response.Write()都能胜任,所以ContentResult显得特别强大:
public override void ExecuteResult(ControllerContext context) { if (context == null) { throw new ArgumentNullException("context"); } HttpResponseBase response = context.HttpContext.Response; if (!String.IsNullOrEmpty(ContentType)) { response.ContentType = ContentType; } if (ContentEncoding != null) { response.ContentEncoding = ContentEncoding; } if (Content != null) { response.Write(Content); } }
若没有提供任何输出的内容,ContentResult呈现的结果与EmptyResult 是一样的,都是输出最基本的<body>标记内容为空的HTML,若内容不为空,则直接输出这些内容(不再输出其他任何 HTML 代码),例如:
public ActionResult Index() { return Content( "a" ); }
事实上从ContentResult我们可以看到一个ActionResult其实并无特别,从前面几个Result 来看,其实不过是Response.Redirect或Response.Write,此外还可以利用二进制流Response.OutputStream.Write向客户端上载文件……据此我们所以拓展编写更多针对实际意义的Result。例如 XmlResult(文件)、RssResult(跟XmlResult其实是一样的)等等。
相关文章推荐
- ASP.NET MVC – 关于Action返回结果类型的事儿(上)
- ASP.NET MVC Action的参数名与参数类型不同,导致取不到Request数据
- ASP.NET MVC 重点教程一周年版 第五回 ActionResult的其它返回值
- ASP.NET MVC – 关于Action返回结果类型的事儿(上)
- ASP.NET MVC ActionResult的其它返回值
- ASP.NET MVC 重点教程一周年版 第五回 ActionResult的其它返回值
- ASP.NET MVC – 关于Action返回结果类型的事儿(上)
- ASP.NET MVC 重点教程一周年版 第五回 ActionResult的其它返回值
- ASP.NET MVC 重点教程一周年版 第五回 ActionResult的其它返回值
- ASP.NET MVC 第五回 ActionResult的其它返回值
- ASP.NET MVC 重点教程一周年版 第五回 ActionResult的其它返回值
- ASP.NET MVC中Controller返回值类型ActionResult
- ASP.NET MVC Action以强类型的方式接收表单数据
- Asp.net MVC中Controller返回值类型ActionResult
- Asp.net Mvc Codeplex Preview 5 第三篇 实现Action参数传递繁杂类型 【转】
- Asp.net MVC 中Controller返回值类型ActionResult
- ASP.NET MVC中常用的ActionResult类型
- ASP.NET MVC – 关于Action返回结果类型的事儿(上)
- ASP.NET MVC中常用的ActionResult类型
- Asp.net MVC 中Controller返回值类型ActionResult