简体   繁体   中英

Is there anyway to modify request body before it gets bind to controller action method in asp.net web api 2

i am trying to send encrypted request to asp.net web api and want web api to intercept the request and decrypt or modify it before it gets bind to controller action method.

Any help will be appreciate.

What you could do is write your custom filter. Since you want to decode the body content before it gets bind, you will have to use AuthorizeAttribute.

public class DecryptRequestContent : System.Web.Http.AuthorizeAttribute
{
    public override void OnAuthorization(HttpActionContext actionContext)
    {
        var requestContent = actionContext.Request.Content;
        var newContent = Decryption (requestContent);
        actionContext.Request.Content = newContent;
    }
}

After that, you will have to decorate your API with this filter like this:

public class SomeController : ApiController
{
    [DecryptRequestContent]
    public void SomeMethod(DataModel model)
    {
        // implementation goes here
    }
}

I think the solution is

actionContext.Request.Content = new StringContent(newContent,Encoding.UTF8,"application/json");

Hopefully it can help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM