How to make a filter before performing action?

Asked

Viewed 181 times

-1

Before executing action, I want to make a filter to check if the value exists.

If value does not exist, return to specific page(Index, Home).

Some solution ?

  • Could you better illustrate what you want? The way you’re getting a little confused

  • It’s still a bit confusing. What do you want to call? This will be for all controllers?

  • I believe what you need is to create a filter that will run before each controller action. see this article, it might help you: http://www.eduardopires.net.br/2013/asp-net-mvc-action-filters-understandingcustomization/ search by Filters action if you prefer

  • Your question was a bit confused, but it tests the code I posted in the answer, here it worked right.

1 answer

1


You can use Action Filters to do this. An Action Filter is an attribute that you put on top of each action (or in the controller, to apply to all actions) indicating to the framework that, before executing the action, it must run its filter. Test the code below:

Home controller code:

using MvcApplication1.ActionFilters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication1.Controllers
{

    public class HomeController : Controller
    {

        [MeuFilter]
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

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

            return View();
        }
    }
}

Filter Code:

using System;
using System.Diagnostics;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcApplication1.ActionFilters
{
    public class MeuFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpSessionStateBase session = filterContext.HttpContext.Session;
            Controller controller = filterContext.Controller as Controller;

            if (controller != null)
            {
                if (session["Login"] == null)
                {
                    controller.HttpContext.Response.Redirect("/Home/About");
                }
            }

        }
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            // Implementar
        }
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            // Implementar
        }
        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            //Implementar
        }
    }
}

Before executing the Index action, the "Meufiltro" will be executed, and in it I am checking a login session, if it does not exist, redirects to another action. To create an Action Filter you need to inherit the Actionfilterattribute class and implement the Iactionfilter interface. For more details I suggest reading that article

Browser other questions tagged

You are not signed in. Login or sign up in order to post.