IActionResult in Asp.net Core MVC VS2017

Asp.net Mvc controllers can have different methods usually called actions or action methods. Asp.net Mvc has introduced the separation of concern with IActionResults. The separation is between stating the intentions and executing the intentions. Stating the intention is to the programmers writing codes in action methods. Execution is usually handled by the built-in IActionResult implementations by the Asp.net Core Mvc.

Separating the intent also helps in unit testing of controllers as you are not exactly executing it when you return the IActionResult this result is still testable and verifiable.

Lets get into the details.

Return Type of Action Methods

Asp.net Core Mvc do not enforce the return type of action methods to be IActionResult always. You can put void, string, string[], Object etc. Default content negotiation will be triggered and it will try to match the response according to the accept header. As Asp.net Core Mvc preferred negotiation type is Json, objects and arrays are usually converted into json objects and arrays implicitly. For strings, plain text mime type will be preferred.

It serves the purpose in different cases but for better control and complex cases we will need to write more code and managing the details in each action method can become frustrating.

Asp.net Mvc offers IActionResult interface for better control. There are number of default implementations of IActionResult interface already available in Asp.net Core Mvc.

Redirecting from Action Methods

Number of times we take decisions in our controllers and decide to redirect the user to some other controller/path to serve the users need. For example a common case can be of session timed out. Whenever your user’s session is timed out you will ask him to login again.  For that you will redirect him to the login page.

302 Redirection

302 redirection is temporary redirection. Most of the time when you redirect the user to another page you knowingly or unknowingly use temporary redirection. ControllerBase class has implementations of few redirect methods.

Redirect();

Above methods return RedirectResult which implements IActionResults.

301 Redirection

301 redirection is not very common to use. It is permanent redirection and to be used very carefully. Once a browser request a URL lets say abc.com/news and Asp.net Mvc redirects it permanently to xyz.com/news, the browser will cache this information with no expiration time. Whenever user will try to browse abc.com/news again the browser will navigate him to xyz.com/news as browser had 301 redirect information already in its cache.

ControllerBase class has following methods implementation for the your ease,

RedirectPermanent();

Calling these methods will redirect the user with header information 301 so that browsers can cache this information for future use. These methods also return RedirectResult which implements IActionResults.

There are other default RedirectResult objects available in Asp.net Core Mvc. You can also create your own extension method to return your own IActionResult implementation based on your needs.

Returning Views

As explained in the beginning the separation of concern enabled the developers to achieve different results more easily and manage them separately. You can state your intention to respond with an HTML view. The creation of the view and rendering is not your headache. Usually views are created using Razor Engine. But since the process of creation of HTML is separated you can use any engine or even create your custom engine to generate HTML views. ControllerBase class provides an implementation to load Razor views.

View();

View() has multiple overloads to get information regarding relative path of the razor file, data to be passed on to the view etc. View() method returns a ViewResult object which implements IActionResult.

Other IActionResults

There are number of default implementations i.e., i could count 38 to 40 implementations available in Asp.net Core Mvc. Few important ones are

JsonResult
ContentResult
ObjectResult
OkObjectResult
NotFoundObjectResult

To send files in response to your action, you can use

FileContentResult
FileStreamResult
VirtualFileResult
PhysicalFileResult

The Interface

Definition of IActionResult is as follows

Any class that implements the interface and provide a definition for

Task ExecuteResultAsync(ActionContext context);

can be returned from the action method. The separation of stating the intention and execution the intention will be maintained and ease of code management will be achieved. Lets create your own simplest implementation of IActionResult.

You have full control over the response object in your own implementation of IActionResult and it’s not blotting your action methods too. You can use your own IActionResult as follows.

calling the action method FileFound will throw 404 error on the browser.

Its always better to provide an extension method on BaseController if you really need to add a new implementation of IActionResult to make it easy to use and available in all future controllers.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s