开发者

How can I remove magic strings from custom model binders?

开发者 https://www.devze.com 2023-01-18 20:06 出处:网络
I\'ve written a couple of custom model binders now, and have realised that I\'ve fallen into the trap of relying on magic strings, e.g.:

I've written a couple of custom model binders now, and have realised that I've fallen into the trap of relying on magic strings, e.g.:

    if (bindingContext.ValueProvider.ContainsP开发者_运维百科refix("PaymentKey"))
    {
        paymentKey = bindingContext.ValueProvider.GetValue("PaymentKey").AttemptedValue;
    }

I'd like to be able to use an expression to strongly-type the prefix names, but can't figure out how, and would be grateful for some assistance.

Thanks.


What you are looking for is bindingContext.ModelName so your code could become:

 if (bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName))
    {
        paymentKey = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue;
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消