Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

ASP.NET MVC Action Filters: HandleError, Authorize and OutputCache

Action Filter is an attribute that when added to an action of a controller, change the way their action is performed. In this article we will see some features about ASP.NET Action Filters

Action Filter is an attribute that when added to an action of a controller, change the way their action is performed.


HandleError


Action Filter HandleError is used to redirect to a custom error page when an error is triggered by the action of the controller.


Listing 1: HandleError example



	[HandleError]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        throw new NullReferenceException();
    } 
    public ActionResult About()
    {
        return View();
    }
}



When the NullReferenceException error happens in the action Index, the ASP.NET will find in a view called “Error”, in the controller views folder and renders it to the user. When the view called “Error” stay in the “Shared” Folder, will be shared with all controllers application.


We can also, redirect to the error pages, each type of error:


Listing 2: Redirect example



[HandleError(ExceptionType = typeof(NullReferenceException),
     View = "NullError")]
[HandleError(ExceptionType = typeof(SecurityException),
     View = "SecurityError")]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        throw new NullReferenceException();
    } 
    public ActionResult About()
    {
        return View();
    }
}


Authorize


This Action Filter is one of the most importants, because is used to define the guidelines of the application security, with him is possible to define how many users or groups of users will have access to specific controller actions.


Listing 3: Only logged users can join in the action About()



public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    } 
    [Authorize]
    public ActionResult About()
    {
        return View();
    }
}

Is also possible define a security system based on users groups:


Listing 4: Only users from Admin and Funcionarios group have access to the action About()



public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    } 
    [Authorize(Roles="Admin, Funcionarios")]
    public ActionResult About()
    {
        return View();
    }
}

Listing 5: Only Pedro and Olga can access the action About()


	
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    } 
    [Authorize(Users = "Pedro, Olga")]
    public ActionResult About()
    {
        return View();
    }
}

OutputCache


This ActionFilter enables the cache of controller actions, useful when specific pages don't change very often and makes it unnecessary to make the whole process several times.


Listing 6: OutputCache example with 15 seconds of duration


public class HomeController : Controller
{
    [OutputCache(Duration = 15)]
    public ActionResult Index()
    {
        return DateTime.Now;
    } 
    public ActionResult About()
    {
        return View();
    }
}

When the action above is called, the screen will show the current time, and in about 15 seconds you’ll see the same date and hour, because the action result was placed in the cache and is reused instead of calling the action again, if this Action Filter is used correctly, can have a significant improvement in application performance.




.Net Developer certified in Microsoft technologies. Net Framework 4.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login