You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core MVC中是否可使用DefaultModelBinder加密解密Action参数?

我想为.NET Core MVC的Action参数自定义一个模型绑定器,但不确定DefaultModelBinder在.NET Core中是否可用,以下是我在.NET Standard环境下写的自定义模型绑定器示例代码:

public class EncryptDataBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        object result = 0;
        if (bindingContext.ModelType == typeof(int))
        {
            var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
            if (valueProviderResult != null)
            {
             // decryption code                    
            }
        }
    return base.BindModel(controllerContext, bindingContext);
   }   
}
.NET Core MVC 自定义模型绑定器:替代DefaultModelBinder的方案

DefaultModelBinder在.NET Core MVC中已被移除,.NET Core采用了全新的模型绑定体系,自定义绑定器需要通过实现IModelBinder接口或继承ModelBinder基类来开发。

针对你提供的.NET Standard版本代码,下面是适配.NET Core MVC的实现:

public class EncryptDataBinder : IModelBinder
{
    public Task BindModelAsync(ModelBindingContext bindingContext)
    {
        // 仅处理int类型的绑定
        if (bindingContext.ModelType != typeof(int))
        {
            bindingContext.Result = ModelBindingResult.Failed();
            return Task.CompletedTask;
        }

        var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        if (valueProviderResult == ValueProviderResult.None)
        {
            bindingContext.Result = ModelBindingResult.Failed();
            return Task.CompletedTask;
        }

        // 执行解密逻辑(替换为你的实际解密代码)
        string encryptedValue = valueProviderResult.FirstValue;
        int decryptedValue = DecryptInt(encryptedValue); // 示例解密方法

        bindingContext.Result = ModelBindingResult.Success(decryptedValue);
        return Task.CompletedTask;
    }

    // 示例解密方法,根据你的加密规则实现
    private int DecryptInt(string encryptedValue)
    {
        // 这里写你的解密逻辑
        return int.Parse(encryptedValue); // 仅为示例,实际需替换
    }
}

核心差异说明:

  • .NET Core中不再使用DefaultModelBinder,转而实现异步的IModelBinder接口,核心方法为BindModelAsync
  • 模型绑定上下文ModelBindingContext整合了原ControllerContext的信息,无需单独传入
  • 通过bindingContext.Result明确设置绑定结果,成功用ModelBindingResult.Success,失败用ModelBindingResult.Failed

绑定器注册方式:

1. 属性标记注册

在Action参数上直接标记绑定器:

public IActionResult Index([ModelBinder(BinderType = typeof(EncryptDataBinder))] int id)
{
    // 业务逻辑
    return View();
}

2. 全局注册

如果需要全局应用该绑定器,可通过模型绑定器提供者注册:

// .NET 6+ Program.cs示例
builder.Services.AddControllers(options =>
{
    options.ModelBinderProviders.Insert(0, new EncryptDataBinderProvider());
});

// 自定义绑定器提供者
public class EncryptDataBinderProvider : IModelBinderProvider
{
    public IModelBinder GetBinder(ModelBinderProviderContext context)
    {
        if (context.Metadata.ModelType == typeof(int))
        {
            return new BinderTypeModelBinder(typeof(EncryptDataBinder));
        }
        return null;
    }
}

内容的提问来源于stack exchange,提问作者JagathNalin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 04:25:17