这可以通过使用ActionFilterAttribute来完成。动作过滤器基本上在动作结果之前或之后与请求相交。因此,我只是为POST操作结果构建了一个自定义操作过滤器属性。这是我所做的:
public class RestAPIAttribute : ActionFilterAttribute{ public override void onActionExecuting(ActionExecutingContext filterContext) { HttpContextbase httpContext = filterContext.HttpContext; if (!httpContext.IsPostNotification) { throw new InvalidOperationException("only POST messages allowed on this resource"); } Stream httpBodyStream = httpContext.Request.InputStream; if (httpBodyStream.Length > int.MaxValue) { throw new ArgumentException("HTTP InputStream too large."); } int streamLength = Convert.ToInt32(httpBodyStream.Length); byte[] byteArray = new byte[streamLength]; const int startAt = 0; httpBodyStream.Read(byteArray, startAt, streamLength); StringBuilder sb = new StringBuilder(); for (int i = 0; i < streamLength; i++) { sb.Append(Convert.ToChar(byteArray[i])); } string xmlBody = sb.ToString(); //Sends XML Data To Model so it could be available on the ActionResult base.onActionExecuting(filterContext); }}然后在控制器上的操作结果方法上,您应该执行以下操作:
[RestAPIAttribute] public ActionResult MyActionResult() { //Gets XML Data From Model and do whatever you want to do with it }希望这对其他人有帮助,如果您认为还有其他更优雅的方法,请告诉我。



