Stats

Actions in ASP.NET MVC 3

A deeper look into different Actions, Action filters, and selectors used inside a Controller.

Introduction

ASP.NET MVC provides a new way of creating web applications which are more extensible and testable. We discussed about ASP.NET MVC in Introduction to ASP.NET MVC 3. Also, we discussed about Routers and Controllers in ASP.NET MVC 3. Here, we will take a deeper look into the different Actions, Action filters, and selectors used inside a Controller.

Action Result

By default, the Controller actions will return the ActionResult object. We can return various types of results as ActionResult, which will decide how the output needs to render on the browser.

public ActionResult About()
{
return View();
}

Sample Controller

For our sample, we will use the following SampleController in addition to the default HomeController and AccountController.

3. RedirectToRoute

When we need to redirect to a route defined in Global.asax, we will use the RedirectToRoute object.

As part of our sample application, we have a custom route defined with the name “sample”. This will route to the Index action inside the Sample Controller. For more on Custom routes, please refer to Controllers and Routers in ASP.NET MVC 3.

public ActionResult Index()
{
return RedirectToRoute("sample");
}

4. File

File is used to return the content of a file to the browser. For our sample, I am returning the web.config to the browser.

This View is defined under Shared Views. We can change the error message using the Error.cshtml file.

Now, let us remove the filter from Global.asax and verify our application. We will receive the following server error:

Now, let us update the CustomError tag to RemoteOnly and verify our application. Now we can see this error message thrown by the application:

We can specify the HandleError filter for an Action or a controller.

ASP.NET MVC defines Action filters like OutputCache, ValidateInput, etc., as part of ASP.NET MVC 3, which we will discuss later.

Action Selectors

ASP.NET MVC 3 defines a set of Action selectors which determine the selection of an Action. One of them is ActionName, used for defining an alias for an Action. When we define an alias for an Action, the Action will be invoked using only the alias; not with the Action name.

Share

About the Author

I have over 10 years of experience working on Microsoft Technologies. I am carrying the passion on Microsoft technologies specifically on web technologies such as ASP.Net, HTML5, jQuery and Ajax. My interests also include TFS, Azure, Windows 8, Visual Studio 2010. Technology adoption and learning is my key strength and technology sharing is my passion.

I need to convert my view to html. i am using document.ready function to initialize the control in default. I used to render the page and conver to html using razor engine and some other tools. But failed to fetch the rendered html. I want to invoke the script function while render. Please help me to find out a solution for the same.