Luke Hammer
Luke Hammer

Reputation: 2228

C# mvc 5 exit if filter condition in Controller

I attempting to Dry out my code. I have a controller that is repeating the same bit of code over and over again but I can't move to a new method because the repeated code contains a return statement.

Here is a code sample

    public class ExampleController : Controller
{

    private Authorizer _authorizer = new Authorizer();

    public ActionResult Edit(int id)
    {
//Repeated Code Start
        var result = _authorizer.EditThing(id);
        if (result.CanEditPartA)
        {
            //log attempted bad access
            //Other repeted things 
            return View("error", result.Message);
        }
//Repeated Code End 

        //unique logic to this action. 
        return View();
    }
    public ActionResult Edit1(int id, FormCollection collection)
    {
//Repeated Code Start
        var result = _authorizer.EditThing(id);
        if (result.CanEditPartA)
        {
            //log attempted bad access
            //Other repeted things 
            return View("error", result.Message);
        }
//Repeated Code End
        //unique logic to this action.

        try
        {

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }
}

public class Authorizer
{
    // check a bunch of things and determine if person can to something

    public Result EditThing(int thingID)
    {
        //get thing from DB
        //logic of thing compared to current user
        //Create Result basied on bussness Logic
        return new Result();
    }


    public class Result
    {
        public bool CanEditPartA { get; set; }
        public bool CanEditPartB { get; set; }
        public bool CanEditPartC { get; set; }
        public string Message { get; set; }
    }
}

}

This example is shortened quite a bit in my real problem the repeated code

 var result = _authorizer.EditThing(id);
        if (result.CanEditPartA)
        {
            //log attempted bad access
            //Other repeted things 
            return View("error", result.Message);
        }

I would like to be able to do something like this

       public ActionResult Edit(int id)
    {

        if (CanUserEditPartA(id) != null)
        {
            return CanUserEditPartA(id);
        }

        //unique logic to this action. 
        return View();
    }

    private ActionResult CanUserEditPartA(int id)
    {
        var result = _authorizer.EditThing(id);
        if (result.CanEditPartA)
        {
            //log attempted bad access
            //Other repeted things 
            return View("error", result.Message);
        }
        return null;
    }

but the problem is when i return null. The methoed Edit exits out also.

Is there a way to have a helper method that returns an ActionResult in some in one path but allows for continues on the main path if null or some other case?

Upvotes: 1

Views: 1722

Answers (1)

T.Aoukar
T.Aoukar

Reputation: 663

This is a proper place to use ActionFilter, by correctly applying it you won't need to even check inside the controller action, if the filter doesn't pass the requirements then it'll set the response and controller action won't be invoked at first place.

Here's a link that can help you: https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/understanding-action-filters-cs

Upvotes: 1

Related Questions