的ViewResult V / S的ActionResult [英] ViewResult V/S ActionResult

查看:107
本文介绍了的ViewResult V / S的ActionResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何决定是否使用的ActionResult或的ViewResult?我知道的ActionResult是一个抽象类用的ViewResult作为亚型但我所看到使用这两种为相同的功能的例子。有什么他们之间的区别?


解决方案

的ActionResult 为基本类型,而的ViewResult 是亚型的ActionResult

当您设置操作的返回类型的ActionResult ,可以返回它的任何亚型例如 JSON,PartialView,景观,RedirectToAction

但是,当你在这种情况下使用的子类型为的ViewResult 你包围你的行动,它只会返回亚型结果在这种情况下是查看

当您使用的ActionResult 作为返回类型,你可以返回从下列行动(这意味着以下是亚型的ActionResult ),如在解释 forums.asp.net


  

    

的ActionResult 是一个普遍的结果类型,可以有若干亚型(从ASP.NET MVC 1.0快速图书):


    
    

A)的ViewResult


    
    

渲染一个specifed以响应流


    
    

B) PartialViewResult


    
    

渲染到响应流的specifed局部视图


    
    

C) EmptyResult


    
    

基本上什么也不做;空给出响应


    
    

D)的 RedirectResult


    
    

执行一个HTTP重定向到URL specifed


    
    

E)的 RedirectToRouteResult


    
    

执行的HTTP重定向,根据给定的路径数据由路由引擎确定一个网址


    
    

F)的 JsonResult


    
    

序列化给定的ViewData对象来JSON格式


    
    

G)的 JavaScriptResult


    
    

返回一段JavaScript代码code,可以上执行
    客户端


    
    

高)的 ContentResult类型


    
    

内容写入到响应流,而不需要一个视图


    
    

我) FileContentResult


    
    

返回FLE到客户端


    
    

j)条的 FileStreamResult


    
    

返回FLE给客户端,这是由一个流

提供
    
    

K)的 FilePathResult


    
    

返回FLE到客户端


  

何时使用的ViewResult

如果您确信您的操作方法将返回一些查看网页,您可以使用的ViewResult 。但是,如果你的操作方法可能有不同的行为,无论是喜欢渲染视图或执行重定向。您可以使用更一般的基类的的ActionResult 作为返回类型。

有关详细信息:

http://forums.asp.net/t/1448398.aspx

您也可以参考这里:

http://www.slideshare.net/umarali1981/difference-between-action-result-and-viewresult

How to decide whether to use ActionResult or ViewResult? I know that ActionResult is an abstract class with ViewResult as a subtype but I have seen examples using both of these for the same functionality. Is there something that differentiates between them?

解决方案

ActionResult is base type while ViewResult is subtype of ActionResult.

When you set Action's return type ActionResult, you can return any subtype of it e.g Json,PartialView,View,RedirectToAction.

But when you use subtype as in this case ViewResult you are bounding your action that it will only return subtype as result which in this case is View.

When you use ActionResult as return type, you can return the following from Action(which means following are the subtypes of ActionResult), as explained on forums.asp.net:

ActionResult is a general result type that can have several subtypes (from "ASP.NET MVC 1.0 Quickly" Book):

a) ViewResult

Renders a specifed view to the response stream

b) PartialViewResult

Renders a specifed partial view to the response stream

c) EmptyResult

Basically does nothing; an empty response is given

d) RedirectResult

Performs an HTTP redirection to a specifed URL

e) RedirectToRouteResult

Performs an HTTP redirection to a URL that is determined by the routing engine, based on given route data

f) JsonResult

Serializes a given ViewData object to JSON format

g) JavaScriptResult

Returns a piece of JavaScript code that can be executed on the client

h) ContentResult

Writes content to the response stream without requiring a view

i) FileContentResult

Returns a fle to the client

j) FileStreamResult

Returns a fle to the client, which is provided by a Stream

k) FilePathResult

Returns a fle to the client

When to Use ViewResult

if you are sure that your action method will return some view page, you can use ViewResult. But if your action method may have different behavior, like either render a view or perform a redirection. You can use the more general base class ActionResult as the return type.

For more details:

http://forums.asp.net/t/1448398.aspx

Also you can refer here:

http://www.slideshare.net/umarali1981/difference-between-action-result-and-viewresult

这篇关于的ViewResult V / S的ActionResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆