0

I want to replace all ۱ chars with 1 in my web api .Net Core requests?
e.g:
Number ۱ should convert to Number 1
In MVC 5 I used HttpModule, in .net core I used Middleware as follows:

namespace VistaBest.Api
{
    public class PersianCharsMiddleware
    {
        private readonly RequestDelegate _next;
        public PersianCharsMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public Task Invoke(HttpContext httpContext)
        {
            var collection = httpContext.Request.Form;
            foreach (var controlKey in collection.Keys.Where(controlKey => !controlKey.StartsWith("__")))
            {
                collection[controlKey] = collection[controlKey].ToString().Replace("۱", "1");
            }
            return _next(httpContext);
        }
    }

    public static class PersianCharsMiddlewareExtensions
    {
        public static IApplicationBuilder UsePersianCharsMiddleware(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<PersianCharsMiddleware>();
        }
    }
}

But collection[controlKey] is Readonly and I can't assign value to it?
Actually, I have to edit all string fields of form, How should I do it?
Can I do it with custom model binder?

3
  • @mjwills I have to standard all numbers chars in string fields and convert them to English Commented Dec 27, 2017 at 12:22
  • 1
    Possible duplicate of How to modify HttpContext.Request.Form in asp.net core Commented Dec 27, 2017 at 12:23
  • I could write it with custom Model Binder, See answer Commented Dec 28, 2017 at 4:15

1 Answer 1

1

I could write it with custom model binder:

public class PersianCharsModelBinder : IModelBinder
{
    private readonly IModelBinder _simpleTypeModelBinder;
    public PersianCharsModelBinder(IModelBinder simpleTypeModelBinder)
    {
        _simpleTypeModelBinder = simpleTypeModelBinder;
    }

    public Task BindModelAsync(ModelBindingContext bindingContext)
    {
        var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        if (valueProviderResult == ValueProviderResult.None) return _simpleTypeModelBinder.BindModelAsync(bindingContext);
        bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);
        var valueAsString = valueProviderResult.FirstValue;
        if (string.IsNullOrWhiteSpace(valueAsString)) return _simpleTypeModelBinder.BindModelAsync(bindingContext);
        var model = valueAsString.ToEnglishNumber().RemoveArabicChars();
        bindingContext.Result = ModelBindingResult.Success(model);
        return Task.CompletedTask;
    }
}

public class PersianCharsBinderProvider : IModelBinderProvider
{
    public IModelBinder GetBinder(ModelBinderProviderContext context)
    {
        if (context == null) throw new ArgumentNullException(nameof(context));
        if (context.Metadata.IsComplexType) return null;
        var simpleTypeModelBinder = new SimpleTypeModelBinder(context.Metadata.ModelType);
        if (context.Metadata.ModelType == typeof(string)) return new PersianCharsModelBinder(simpleTypeModelBinder);
        return simpleTypeModelBinder;
    }
}

Startup.cs

services.AddMvc(options =>
            {
                options.ModelBinderProviders.Insert(0, new PersianCharsBinderProvider());
            });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.