ASP.NET MVC中Action返回值类型
Action的返回值类型一共提供了十几种。简单概括一下其中很常用的几种类型。
1、ActionResult
所有的 Result 都派生自 ActionResult抽象类,因此 ActionResult 作为基类提供了最基础的功能,ActionResult 是一个抽象类,其声明如下:
[csharp]
<SPAN style="FONT-FAMILY: Microsoft YaHei; FONT-SIZE: 14px">public abstract class ActionResult {
public abstract void ExecuteResult(ControllerContext context);
}</SPAN>
public abstract class ActionResult {
public abstract void ExecuteResult(ControllerContext context);
}
2、EmptyResult
[csharp]
<SPAN style="FONT-FAMILY: Microsoft YaHei; FONT-SIZE: 14px">示public class EmptyResult : ActionResult {
private static readonly EmptyResult _singleton = new EmptyResult();
internal static EmptyResult Instance {
get {
return _singleton;
}
}
public override void ExecuteResult(ControllerContext context) {
}
}</SPAN>
示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中,若要返回一个空的页面(不常用),则可如下:
[csharp]
<SPAN style="FONT-FAMILY: Microsoft YaHei; FONT-SIZE: 14px">public ActionResult Index()
{
<SPAN style="WHITE-SPACE: pre"> </SPAN>return new EmptyResult();
}</SPAN>
public ActionResult Index()
{
return new EmptyResult();
}
3、RedirectResult
[csharp] view plaincopyprint?<SPAN style="FONT-FAMILY: Microsoft YaHei; FONT-SIZE: 14px"> 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 */);
}</SPAN>
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):
[csharp]
<SPAN style="FONT-FAMILY: Microsoft YaHei; FONT-SIZE: 14px">
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 */);
}</SPAN>
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显得特别强大:
[csharp]
<SPAN style="FONT-FAMILY: Microsoft YaHei; FONT-SIZE: 14px">
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);
}
}</SPAN>
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 代码),例如:
[csharp]
<SPAN style="FONT-FAMILY: Microsoft YaHei; FONT-SIZE: 14px"> public ActionResult Index()
{
return Content( "a" );
}
</SPAN>
public ActionResult Index()
{
return Content( "a" );
}
事实上从ContentResult我们可以看到一个ActionResult其实并无特别,从前面几个Result 来看,其实不过是Response.Redirect或Response.Write,此外还可以利用二进制流Response.OutputStream.Write向客户端上载文件……据此我们所以拓展编写更多针对实际意义的Result。例如 XmlResult(文件)、RssResult(跟XmlResult其实是一样的)等等。
上一篇: CentOS安装redis.tar.gz
下一篇: 一生历经三任皇帝,冯太后是个怎样的女子?