Force two ActionFilter attributes to be used in conjunction with one another

92 views Asked by At

I had three Authorization Attributes that contained code to check that the content being requested actually exists. I decided to avoid repeating myself in each, by creating a new attribute called QuizExistsAttribute.

I wish to run this before my other Authorization attributes.

Now that I have this extra attribute, I wish to ensure that the original attributes that I was using, aren't used without this new attribute - because I want the check to be made before anything else.

My other authorization attributes that rely on the logic of QuizExistsAttribute being performed are:

  • ActiveQuizTakerSessionAtrribute
  • AuthorizeQuizAdminAttribute

So in my code I will be using them like so:

/// <summary>
/// Start of a quiz
/// </summary>
/// <param name="urlId"></param>
/// <returns></returns>
[QuizExists] // Check that quiz exists
[ActiveQuizTakerSession] // Check that they have an active session for this quiz
[HttpGet]
public ActionResult QuizQuestion(string urlId)
{
    // Code here after checks
}

Is there a way to enforce that ActiveQuizTakerSession is used injunction with (and following) the QuizExists attribute?

1

There are 1 answers

0
Jason Evans On

You can set the order in which each attribute is executed:

[QuizExists(Order = 1)] // Check that quiz exists
[ActiveQuizTakerSession(Order = 2)] // Check that they have an active session for this quiz
[HttpGet]

Here's a good reference to help you further:

Order property of ActionFilter, from lowest to greatest or vice versa?