Asp.net-Mvc

如何將 XML 作為 POST 傳遞給 ASP MVC .NET 中的 ActionResult

  • January 19, 2016

我正在嘗試為我的 ASP MVC 項目提供一個簡單的 RESTful API。我將無法控制此 API 的客戶端,它們將通過 POST 方法傳遞 XML,該方法將包含在伺服器端執行某些操作所需的資訊,並返回帶有操作結果的 XML。我發回 XML 沒有問題,問題是通過 POST 接收 XML。我看過一些 JSON 範例,但由於我不會控制我的客戶端(從我的角度來看,它甚至可能是一個 telnet)我認為 JSON 不會起作用。我對麼?

我見過一些範例,其中客戶端只需將正確的表單格式構造為請求正文的一部分,然後 ASP 解析消息,並且數據可作為 FormCollection (?param1=value1¶m2=value2& 等) 使用。但是,我想將純 XML 作為消息正文的一部分傳遞。

感謝您的幫助,

這可以通過使用 ActionFilterAttribute 來完成。Action Filters 基本上與 Action Result 之前或之後的請求相交。所以我剛剛為 POST Action Result 建構了一個自定義操作過濾器屬性。這是我所做的:

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;

       /*
        * Copies the stream into a byte array
        */
       httpBodyStream.Read(byteArray, startAt, streamLength);

       /*
        * Convert the byte array into a string
        */
       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
   }

希望這對其他人有所幫助,如果您認為有更優雅的方法可以做到這一點,請告訴我。

@Freddy - 喜歡您的方法並使用以下程式碼對其進行了改進以簡化流讀取:

   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.");
       }

       StreamReader reader = new StreamReader(httpBodyStream, Encoding.UTF8);
       string xmlBody = reader.ReadToEnd();
       reader.Close();

       filterContext.ActionParameters["message"] = xmlBody;

       // Sends XML Data To Model so it could be available on the ActionResult
       base.OnActionExecuting(filterContext);
   }

然後在控制器中,您可以將 xml 作為字元串訪問:

[RestAPIAttribute]    
public ActionResult MyActionResult(string message)    
{         

}

引用自:https://stackoverflow.com/questions/1111874