[英]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.我正在尝试向 asp.net web api 发送加密请求,并希望 web api 拦截请求并在它绑定到控制器操作方法之前对其进行解密或修改。
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.
由于您想在绑定之前对正文内容进行解码,因此您必须使用 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:之后,您将必须使用此过滤器来装饰您的 API,如下所示:
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!希望它可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.