몇 가지 비슷한 질문을 본 적이 있지만 해결 방법이 없습니다.웹 API 게시 XML 객체는 항상 null입니다.
이 코드는 JSON 페이로드를 전달할 때 완벽하게 작동합니다. 그러나 XML로드의 경우 컨트롤러 POST 메소드에 전달 된 객체는 항상 NULL입니다.
내 콘솔 앱은 내 ASP MVC 컨트롤러에 POST HTTP 요청을 보내고이 시점에서 전달 된 XML 페이로드 개체는 항상 null입니다.
이
는 HTTP 요청 객체를 보여주는 내 콘솔 응용 프로그램 설정되고 있으며 XML하여 XDocument 페이로드가 건설되고 :[System.Web.Mvc.HttpPost]
public string PostXML(object xmlPayload)
{
//*****The 'xmlPayLoad' object is always null*****
List<SplitXML> returnedPopulatedXMLObject = SetXMLstrings(XDocument.Parse(xmlPayload.ToString()));
foreach (var XMLitem in returnedPopulatedXMLObject)
{
_ClientID = XMLitem.ISIClientID;
_OrganizationID = XMLitem.OrganizationID;
_Status = XMLitem.Status;
}
string stringXDoc;
return stringXDoc = "POSTed";
}
이는 다음과 같습니다
XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
XElement clientRequestElement = new XElement(xmlns + "PLTSActivation",
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XElement(xmlns + "iSIClientID", iid.ToString()),
new XElement(xmlns + "organizationId", ooid),
new XElement(xmlns + "statusDescription", "Success"));
doc.Add(clientRequestElement);
return doc.ToString(SaveOptions.DisableFormatting);
}
public static void PostXML()
{
string _URL = "http://localhost:24689/api/PTShandler/postxml";
string _OOID = "TEST";
string convertedJSONPayload = "";
var payloadObj = new
{
XmlPayload = ConstructProvisioningRequest(99783971, _OOID)
};
try
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(_URL);
httpWebRequest.Headers.Add("ORGOID", _OOID);
httpWebRequest.Headers.Add("Culture", "en-US");
httpWebRequest.ContentType = "application/xml";
httpWebRequest.Accept = "application/xml";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(payloadObj.ToString());
streamWriter.Flush();
streamWriter.Close();
}
}
이 내 PTShandler 컨트롤러가 POST 방법을 보여주고있다 내 Global.asax.cs 파일 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace RESTful.CallBackService
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
그리고 내 App_Start WebApi입니다. Config.cs 파일 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Xml.Serialization;
namespace RESTful.CallBackService
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new
{
id = RouteParameter.Optional
}
);
}
}
}
어디에서 잘못 된 지 알 수 있습니까?
[피들러] (http://fiddler2.com/)를 발사하고 HTTP를 검사 해 보셨습니까? – Liam