简体   繁体   English

在它绑定到 asp.net web api 2 中的控制器操作方法之前,无论如何要修改请求正文

[英]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.

相关问题 ASP.NET Web API请求未达到控制器操作 - ASP.NET Web API request does not reach controller action 是否有一种开箱即用的方式将 HTTP 请求的整个主体绑定到 ASP.NET Core 控制器操作中的字符串参数? - Is there an out of the box way to bind the entire body of an HTTP request to a string parameter in an ASP.NET Core controller action? 如何在 ASP.NET Core 5 Web API 中绑定来自请求主体的简单类型 - How can I bind simple type coming from body of the request in ASP.NET Core 5 Web API asp.net Web api:将请求正文值绑定到模型类 - asp.net web api : bind request body values to the model class 在asp.net Web API中反序列化之前验证JSON - Validating JSON before it gets deserialized in asp.net web api 在ASP.NET Core Web API中使用依赖项注入时未调用Controller Action方法 - Controller Action method not called while using dependency injection in asp.net core web api ASP.Net 中具有相同路由名称的 Post Action 方法重载 Web API Controller - Post Action method overload with same route name in ASP.Net Web API Controller 向ASP.NET MVC控制器中的操作方法发出HttpPost请求 - Make HttpPost request to an action method in an ASP.NET MVC controller 在ASP.NET Web Api中获取请求正文 - Get body of request in ASP.NET Web Api 向ASP.NET Web API Controller添加显式操作路由 - Adding an explicit action route to ASP.NET Web API Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM