当前位置:编程学习 > asp >>

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]
<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
ContentResul

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,