Action Result

of ASP.NET Core (.Net 6) — 02

When you will be working with a .Net core application, you will see the return type of action result. And it does not matter if you are using an MVC application or a Razor page application. In both cases, you can see we have an action result. In MVC, we are returning back to the page by View(). And in Razor, we return by Page(). But the return type is IActionResult. IActionResult is a generic (通用) type that implements all of the other return types.

Now if you want to be explicit about the return type in both of these cases, then that would look like the following image. If the return type is View, you can write ViewResult (see red arrow). But on the Razor page, the return type is Page, the return type is PageResult.

So, what is the advantage of action results? Let’s first know some theories.

  1. ActionResult is a result of action methods/pages or return types of action methods/page handlers. (ActionResult 是一個方法/頁面的執行結果、或是方法/頁面處理程序的回傳類型)

Let’s look at the below table is about the ActionResult in Razor Pages.

If you are working with the MVC application, we can return back viewed partial things. So if you are returning any one of these and you can use the individual return typed or preserve based on the helper method.

But what if you were returning something like this? Right here in MVC, you can see we are returning something like below. Either in MVC ViewResult/RedirectToActionResult or in Razor PageResult/RedirectToPageResult, the return result only exists one but two.

The solution is to use IActionResult because this is a better class. Because it is a foundation so it does not care which of its implementation is being returned. It will be able to handle all of them.

--

--

Code Newbie

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store