Blu*_*ode 10 .net asp.net security asp.net-mvc dependency-management
我们已将业务逻辑层和业务对象分离为完全独立的项目/程序集.模型的某些属性可以包含HTML内容.在业务逻辑的前面,我们有一个ASP.NET MVC Web应用程序,用户可以在其中管理业务对象.
那么,我们如何向MVC模型绑定器指出我们想要在某些特定属性上允许HTML内容(并且仅在其上),而不在我们的业务逻辑层中引用ASP.NET MVC?或者,如何在没有强引用的情况下从另一个程序集注入元数据?
谢谢.
我必须将BindModel更改为以下(这是建立在Russ Cam的答案上),以便检查实际属性的属性.我也看了这个答案寻求帮助:
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var holderType = bindingContext.ModelMetadata.ContainerType;
if (holderType != null)
{
var propertyType = holderType.GetProperty(bindingContext.ModelMetadata.PropertyName);
var attributes = propertyType.GetCustomAttributes(true);
var hasAttribute = attributes
.Cast<Attribute>()
.Any(a => a.GetType().IsEquivalentTo(typeof(MyAllowHtmlAttribute)));
if (hasAttribute)
{
bindingContext.ModelMetadata.RequestValidationEnabled = false;
}
}
return base.BindModel(controllerContext, bindingContext);
}
Run Code Online (Sandbox Code Playgroud)
AllowHtml 依赖的请求验证概念,以及绑定检查特定于 Web 请求。这里的关注点没有分离,它们是紧密相连的。所以不,如果不参考 System.Web 等,您就无法使用它。
您排除了(在我看来)最正确的选项 - 视图模型,即使验证和绑定实际上是一个视图模型概念。
您无法拥有具有特定于 Web 的绑定和验证概念的可移植业务对象。
归档时间: |
|
查看次数: |
2502 次 |
最近记录: |