locked
how to inject service to custom model binder as a dependency without adding the references RRS feed

  • Question

  • User-1353133982 posted

    I have a generic custom ModelBinder class that has a reference to a HTMLSanitizer service to sanitise the string values. I want to use this service as a dependency instaed of tightly coupling with my code. I am unsure if this is possible or not. I am using ASP.net Core 3.1

     public Task BindModelAsync(ModelBindingContext bindingContext) {
                if (bindingContext == null)
                    throw new ArgumentNullException(nameof(bindingContext));
                var htmlSanitizer = bindingContext.HttpContext.RequestServices.GetService<IHtmlSanitizer>();
                var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
    
                if (valueProviderResult == ValueProviderResult.None) {
                    return _fallbackBinder.BindModelAsync(bindingContext);
                }
    
                var valueAsString = valueProviderResult.FirstValue;
    
                if (string.IsNullOrEmpty(valueAsString)) {
                    return _fallbackBinder.BindModelAsync(bindingContext);
                }
                var result = htmlSanitizer == null ? HtmlEncoder.Default.Encode(valueAsString) : htmlSanitizer.Sanitize(valueAsString);
                bindingContext.Result = ModelBindingResult.Success(result);
    
                return Task.CompletedTask;
            }
    

    Is there a way to get the service without adding reference of "<IHtmlSanitizer>" directly? is there a way that i can read service from startup.cs  

    Thursday, June 25, 2020 9:26 PM

All replies

  • User1120430333 posted

    You should be able to do this in the Configuration() in startup.cs

    services.AddSingleton<IHtmlSanitizer, HtmlSanitizer>();

    And then you should be able to DI on the HtmlEncodeModelBinder class constructor.

    You can reference the link.

    https://stackoverflow.com/questions/58065590/htmlsanitizer-asp-net-core-2-with-di

    Thursday, June 25, 2020 10:31 PM
  • User-1353133982 posted

    You should be able to do this in the Configuration() in startup.cs

    services.AddSingleton<IHtmlSanitizer, HtmlSanitizer>();

    And then you should be able to DI on the HtmlEncodeModelBinder class constructor.

    You can reference the link.

    https://stackoverflow.com/questions/58065590/htmlsanitizer-asp-net-core-2-with-di

    Thank you, I already came across this thread, actually i am looking how will it look like on the ModelBinder side? as i want to make it as non dependable( only if user wants they can inject). I am not sure how to do that at modelbinder side.

    Friday, June 26, 2020 1:01 AM
  • User-2054057000 posted

    DA924

    You should be able to do this in the Configuration() in startup.cs
    services.AddSingleton<IHtmlSanitizer, HtmlSanitizer>();
    And then you should be able to DI on the HtmlEncodeModelBinder class constructor.
    You can reference the link.
    https://stackoverflow.com/questions/58065590/htmlsanitizer-asp-net-core-2-with-di

    Thank you, I already came across this thread, actually i am looking how will it look like on the ModelBinder side? as i want to make it as non dependable( only if user wants they can inject). I am not sure how to do that at modelbinder side.

    It will only be injected if there is a dependency on the controller. Refer how Dependency Injection in ASP.NET Core works in different situations in this article.

    Friday, June 26, 2020 5:33 AM
  • User-474980206 posted

    If you don’t know the interface, how do you call it? 

    Saturday, June 27, 2020 3:49 PM