-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
45 additions
and
43 deletions.
There are no files selected for viewing
72 changes: 36 additions & 36 deletions
72
NewLife.Remoting.Extensions/ModelBinders/InterfaceModelBinder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,51 @@ | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
|
||
namespace NewLife.Remoting.Extensions.ModelBinders | ||
namespace NewLife.Remoting.Extensions.ModelBinders; | ||
|
||
/// <summary>接口模型绑定器</summary> | ||
public class InterfaceModelBinder : IModelBinder | ||
{ | ||
/// <summary>接口模型绑定器</summary> | ||
public class InterfaceModelBinder : IModelBinder | ||
/// <summary>对于Json请求,从body中读取参数</summary> | ||
/// <param name="bindingContext"></param> | ||
/// <returns></returns> | ||
public async Task BindModelAsync(ModelBindingContext bindingContext) | ||
{ | ||
/// <summary>对于Json请求,从body中读取参数</summary> | ||
/// <param name="bindingContext"></param> | ||
/// <returns></returns> | ||
public async Task BindModelAsync(ModelBindingContext bindingContext) | ||
var provider = bindingContext.HttpContext.RequestServices; | ||
var modelType = bindingContext.ModelType; | ||
|
||
// 从容器中获取接口类型对应实例 | ||
var model = provider.GetRequiredService(modelType); | ||
|
||
try | ||
{ | ||
var req = bindingContext.HttpContext.Request; | ||
var provider = bindingContext.HttpContext.RequestServices; | ||
var modelType = bindingContext.ModelType; | ||
// 从容器中获取接口类型对应实例 | ||
var model = provider.GetService(modelType); | ||
|
||
try | ||
{ | ||
var entityBody = await req.ReadFromJsonAsync(model.GetType()); | ||
|
||
bindingContext.Result = ModelBindingResult.Success(entityBody); | ||
} | ||
catch (Exception ex) | ||
{ | ||
bindingContext.ModelState.AddModelError(bindingContext.ModelName, ex.Message); | ||
} | ||
var entityBody = await req.ReadFromJsonAsync(model!.GetType()); | ||
|
||
bindingContext.Result = ModelBindingResult.Success(entityBody); | ||
} | ||
catch (Exception ex) | ||
{ | ||
bindingContext.ModelState.AddModelError(bindingContext.ModelName, ex.Message); | ||
} | ||
} | ||
} | ||
|
||
/// <summary>模型绑定器提供者</summary> | ||
public class InterfaceModelBinderProvider : IModelBinderProvider | ||
/// <summary>模型绑定器提供者</summary> | ||
public class InterfaceModelBinderProvider : IModelBinderProvider | ||
{ | ||
/// <summary>获取绑定器</summary> | ||
/// <param name="context"></param> | ||
/// <returns></returns> | ||
public IModelBinder? GetBinder(ModelBinderProviderContext context) | ||
{ | ||
/// <summary>获取绑定器</summary> | ||
/// <param name="context"></param> | ||
/// <returns></returns> | ||
public IModelBinder? GetBinder(ModelBinderProviderContext context) | ||
{ | ||
var modelType = context.Metadata.ModelType; | ||
|
||
if (modelType.IsInterface) | ||
{ | ||
return new InterfaceModelBinder(); | ||
} | ||
if (!context.Metadata.IsComplexType) return null; | ||
|
||
return null; | ||
var type = context.Metadata.ModelType; | ||
if (type.IsInterface && context.Services?.GetService(type) != null) | ||
{ | ||
return new InterfaceModelBinder(); | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters