btcpayserver/BTCPayServer/ModelBinders/WalletIdModelBinder.cs

38 lines
1.1 KiB
C#
Raw Normal View History

2018-07-26 15:32:24 +02:00
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace BTCPayServer.ModelBinders
{
public class WalletIdModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (!typeof(WalletId).GetTypeInfo().IsAssignableFrom(bindingContext.ModelType))
{
return Task.CompletedTask;
}
ValueProviderResult val = bindingContext.ValueProvider.GetValue(
bindingContext.ModelName);
string key = val.FirstValue as string;
if (key == null)
{
return Task.CompletedTask;
}
2020-06-28 10:55:27 +02:00
if (WalletId.TryParse(key, out var walletId))
2018-07-26 15:32:24 +02:00
{
bindingContext.Result = ModelBindingResult.Success(walletId);
}
else
{
bindingContext.Result = ModelBindingResult.Failed();
bindingContext.ModelState.AddModelError(bindingContext.ModelName, "Invalid wallet id");
}
2018-07-26 15:32:24 +02:00
return Task.CompletedTask;
}
}
}