How to get hold of Content that is already read

前端 未结 4 1473
小鲜肉
小鲜肉 2020-12-01 06:34

I have a class that inherits from ApiController. It has a Put-method like this:

[PUT(\"user/{UserId}\")]
public HttpResponseMessage Put(string userId, Payme         


        
4条回答
  •  醉话见心
    2020-12-01 07:30

    A very delayed response, but recently I have had the same challenge to overcome.

    I have approached that a bit different without having to get the data from the httpContext (can be quite costly for high volume transaction web application).

    I created a simple interface and made each Controller implementing it:

    public interface IBaseControllerData
    {
        object Entity { get; set; }
    }
    

    I have then set the Controller's Entity property to the Json payload for each post and put action. Finally, I retrieved the Entity data within the ActionFilterAttribute.OnActionExecuted overridden method and serialised it to Json before injecting into MongoDB:

    object entity = ((IBaseControllerData)actionExecutedContext.ActionContext.ControllerContext.Controller).Entity;
                        requestBody = Newtonsoft.Json.JsonConvert.SerializeObject(entity);
    

    Hope that helps !

    Cheers

提交回复
热议问题