Part 78 - MVC中不同的ActionResult 类型
2016-11-10 16:10
316 查看
In this section, we will discuss different types of ActionResult objects that can
be returned by an action method.
The following is the signature of a typical action method in a controller. Notice that, the return type is ActionResult. ActionResult
is an abstract class and has several sub types(子类).
![](https://img-blog.csdn.net/20161110163305733?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
I have used a tool called ILSpy, to list all the sub-types of ActionResult. To use the tool yourself, here are the steps
1. Navigate to http://ilspy.net
2. Click on "Download Binaries" button, and extract them to a folder.
3. Run ILSpy.exe which can be found in the folder, where you have extracted the binaries.
4. Click on File - Open From GAC
5. Type "System.Web.Mvc" in the search box. Select the Assembly and click Open
6. At this point System.Web.Mvc assmbly should be loaded into ILSpy. Expand System.Web.Mvc, then expand ActionResult and then expand "Derived Types". You should now be able to see all the
derived types.
Why do we have so many sub-types?
An action method in a controller, can return a wide range of objects. For example, an action method can return
1. ViewResult
2. PartialViewResult
3. JsonResult
4. RedirectResult etc..
What should be the return type of an action method - ActionResult or specific derived type?
It's a good practise to return specific sub-types, but, if different paths of the action method returns different subtypes, then I would return an ActionResult object. An example is shown below.
public ActionResult Index()
{
if (Your_Condition)
return View(); // returns ViewResult object
else
return Json("Data"); // returns JsonResult object
}
The following table lists
1. Action Result Sub Types
2. The purpose of each sub-type
3. The helper methods used to retrun the specific sub-type
be returned by an action method.
The following is the signature of a typical action method in a controller. Notice that, the return type is ActionResult. ActionResult
is an abstract class and has several sub types(子类).
public ActionResult Index() { return View(); }Here is the list of all sub-types of ActionResult.
I have used a tool called ILSpy, to list all the sub-types of ActionResult. To use the tool yourself, here are the steps
1. Navigate to http://ilspy.net
2. Click on "Download Binaries" button, and extract them to a folder.
3. Run ILSpy.exe which can be found in the folder, where you have extracted the binaries.
4. Click on File - Open From GAC
5. Type "System.Web.Mvc" in the search box. Select the Assembly and click Open
6. At this point System.Web.Mvc assmbly should be loaded into ILSpy. Expand System.Web.Mvc, then expand ActionResult and then expand "Derived Types". You should now be able to see all the
derived types.
Why do we have so many sub-types?
An action method in a controller, can return a wide range of objects. For example, an action method can return
1. ViewResult
2. PartialViewResult
3. JsonResult
4. RedirectResult etc..
What should be the return type of an action method - ActionResult or specific derived type?
It's a good practise to return specific sub-types, but, if different paths of the action method returns different subtypes, then I would return an ActionResult object. An example is shown below.
public ActionResult Index()
{
if (Your_Condition)
return View(); // returns ViewResult object
else
return Json("Data"); // returns JsonResult object
}
The following table lists
1. Action Result Sub Types
2. The purpose of each sub-type
3. The helper methods used to retrun the specific sub-type
相关文章推荐
- MVC学习一:MVC Action Result 返回类型
- Asp.net MVC 中Controller返回值类型ActionResult
- Asp.net MVC中Controller返回值类型ActionResult
- MVC学习二:MVC Action Result 返回类型扩展
- ASP.NET MVC中常用的ActionResult类型
- MVC学习一:MVC Action Result 返回类型
- Asp.net MVC 中Controller返回值类型ActionResult
- MVC 返回ActionResult 类型
- ASP.NET MVC 不同的 ActionResult 小示例
- MVC学习二:MVC Action Result 返回类型扩展
- ASP.NET MVC中Controller返回值类型ActionResult
- MVC(六、ActionResult 返回类型 )
- ASP.NET MVC中常用的ActionResult类型
- ASP.NET MVC Action的参数名与参数类型不同,导致取不到Request数据
- Asp.net MVC 中Controller返回值类型ActionResult
- ASP.NET MVC中常用的ActionResult类型
- MVC学习三:MVC Action Result 返回类型实例
- Asp.net MVC 中Controller返回值类型ActionResult
- ASP.NET MVC 不同的 ActionResult 小示例
- 关于asp.net mvc中Controller中的方法返回值类型ActionResult及其子类的理解