Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

When creating a custom ValidationAttribute, the ValidationContext always don't have a valid ServiceContainer #6346

@jlVidal

Description

@jlVidal

Following this article: https://andrewlock.net/injecting-services-into-validationattributes-in-asp-net-core/

When creating custom ValidationAttribute several people already note that is impossible to access/get any service from the GetService method inside the ValidationContext class, ServiceContainer property. Looking into the code is possible to see:

 var context = new ValidationContext( instance: container ?? validationContext.Model, serviceProvider: validationContext.ActionContext?.HttpContext?.RequestServices, items: null) { DisplayName = metadata.GetDisplayName(), MemberName = memberName }; 

https://github.com/aspnet/Mvc/blob/760c8f38678118734399c58c2dac981ea6e47046/src/Microsoft.AspNetCore.Mvc.DataAnnotations/Internal/DataAnnotationsModelValidator.cs

Since the ActionContext was obsolete: https://stackoverflow.com/questions/37286530/actioncontext-gone-in-microsoft-aspnetcore-mvc-controller - The service provider for a validation attribute is always null or empty. Let's make the ValidationAttribute great again, please fix this issue in the next patch.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions