Friday, September 26, 2014

Action Filter in MVC Application

In MVC, Controller defined the action method which  are responsible to  execute the user interaction such as insert from ,update from , get data from database .All that stuff we have handle through controller action method.
Sometime you want to perform the logic either before an action method is called or after an action method runs. To support that Microsoft mvc framework provide the actionfilter attribute. Action filters are custom attributes that provide a declarative means to add pre-action and post-action behavior to controller action methods.
  Authorization filter,
 security decisions about whether to execute an action method, such as performing authentication or validating properties of the request. The AuthorizeAttribute class is one example of an authorization filter is given below

[Authorize]
        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

    Action filter
Which wraps the action method execution. This filter can perform additional processing, such as providing extra data to the action method, inspecting the return value, or canceling execution of the action method.Sometime we need to store the log of user or handle the error
[myAction]
        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";//dynamic lis
            ViewData["Message"] = "Here is tesing";
           

            return View();
        }
   Result filter
Which wraps execution of the ActionResult object. This filter can perform additional processing of the result, such as modifying the HTTP response. The OutputCacheAttribute class is one example of a result filter.
  Exception filter
which executes if there is an unhandled exception thrown somewhere in action method, starting with the authorization filters and ending with the execution of the result. Exception filters can be used for tasks such as logging or displaying an error page. The HandleErrorAttribute class is one example of an exception filter.
[HandleError]
        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";//dynamic lis
            ViewData["Message"] = "Here is tesing";
           

            return View();
        }

No comments:

Post a Comment

http://blogsiteslist.com