ASP.NET Web Api中的自定义参数BindingAttribute
如何实现自定义ASP.NET Web Api中的自定义参数BindingAttribute,binding,asp.net-web-api,Binding,Asp.net Web Api,如何实现自定义参数bindingAttribute类似[FromBody] // POST api/<controller> public void Post([FromBody]string value) { } 通过创建自定义BinderType public string Get([FromUri(BinderType = typeof(UrlToFileSystemMapping))]string path = "") { } public
参数bindingAttribute
类似[FromBody]
// POST api/<controller>
public void Post([FromBody]string value)
{
}
通过创建自定义
BinderType
public string Get([FromUri(BinderType = typeof(UrlToFileSystemMapping))]string path = "")
{
}
public class UrlToFileSystemMapping : IModelBinder
{
bool IModelBinder.BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
var val = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (val != null)
{
var s = val.AttemptedValue as string;
if (s != null)
{
bindingContext.Model = s.Replace('|', '\\');
return true;
}
}
return false;
}
}
public string Get([FromUri(BinderType = typeof(UrlToFileSystemMapping))]string path = "")
{
}
public class UrlToFileSystemMapping : IModelBinder
{
bool IModelBinder.BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
var val = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (val != null)
{
var s = val.AttemptedValue as string;
if (s != null)
{
bindingContext.Model = s.Replace('|', '\\');
return true;
}
}
return false;
}
}