ADringer
ADringer

Reputation: 2834

Web API verfication of requests

Currently I have a Web API project with FluentValidation tied in to verify the requests that come in. This is working fine to make sure that the requests make sense.

My next step is to verify the request. What I mean by this is some POST (create) requests link to existing entities and may require the following checks:

  1. I need to verify that the linked entities belong to the current user
  2. Check to see if the user already has an 'Active' entity of the same type requested.
  3. Check that the linked entities support the requested entity

How can I be doing these checks? I don't want to tie it into my FluentValidation as this should just validate the requests and I don't want to make trips to the DB if I'm going to return a Bad Request due to validation.

I could add these checks into each method in the controller but that doesn't seem very nice. Is there an Action or something similar that I can plug in which will be called after FluentValidation does it thing but before it hits the controller?

Thanks

Alex

Upvotes: 1

Views: 51

Answers (1)

StriplingWarrior
StriplingWarrior

Reputation: 156524

It is possible to create custom Action Filters to do these checks, but in my experience it doesn't typically make sense to do so unless the thing you're trying to check is applicable to almost every request (e.g. make sure the user is logged in).

I would just put the logic for the kinds of checks you're talking about into separate utility classes where it can be easily reused, and make it the responsibility of each action to call the appropriate utility methods based on what checks need to occur for that action.

Upvotes: 2

Related Questions