4

How can I implement custom ParameterBindingAttribute similar [FromBody]

// POST api/<controller>
public void Post([FromBody]string value)
{
}

For example do something like

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Parameter, Inherited = true, AllowMultiple = false)]
public sealed class EncodeAttribute : ParameterBindingAttribute
{

    public override HttpParameterBinding GetBinding(HttpParameterDescriptor parameter)
    {
        //1.get parameter value
        //2.change value
        //3.bindig value
    }
}

1 Answer 1

3

Solved the problem by creating custom 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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.