Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Access HttpContext from class/model level validation method

$
0
0

So just to give some background I have changed my web application so it doesn't depend on accessing HttpContext.Current from class libraries, instead the entity framework context is passed around the application.

This is all working fine except for one small exception, in methods on models that are for class level validation I can't access HttpContext (as this is only available on controllers) and as the method is called automatically by the inner workdings of MVC I can't add a parameter to the method so i'm stuck.

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
...
  });

How do I access the controller that has called the above function from within the function so I can access the HttpContext and then get the EF context so I can call a method in the business logic layer to check something, is the question? I have looked at the validationContext parameter but can't see a way of getting it from that.

Thanks


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>