Per passare i dati come una puntura in MVC è necessario creare il proprio formattatore di tipo di supporto per gestire il testo normale. Quindi aggiungere il formattatore alla sezione di configurazione.
Per utilizzare il nuovo formattatore specificare il tipo di contenuto per tale formattatore, ad esempio text/plain.formattatore
Esempio per il testo
metodo
using System;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.IO;
using System.Text;
namespace SampleMVC.MediaTypeFormatters
{
public class TextMediaTypeFormmatter : XmlMediaTypeFormatter
{
private const int ByteChunk = 1024;
private UTF8Encoding StringEncoder = new UTF8Encoding();
public TextMediaTypeFormmatter()
{
base.UseXmlSerializer = true;
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
}
public override bool CanReadType(Type type)
{
if (type == typeof(string))
{
return true;
}
return false;
}
public override bool CanWriteType(Type type)
{
if (type == typeof(string))
{
return true;
}
return false;
}
public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, System.Net.Http.HttpContent content, IFormatterLogger formatterLogger)
{
StringBuilder StringData = new StringBuilder();
byte[] StringBuffer = new byte[ByteChunk];
int BytesRead = 0;
Task<int> BytesReadTask = readStream.ReadAsync(StringBuffer, 0, ByteChunk);
BytesReadTask.Wait();
BytesRead = BytesReadTask.Result;
while (BytesRead != 0)
{
StringData.Append(StringEncoder.GetString(StringBuffer, 0, BytesRead));
BytesReadTask = readStream.ReadAsync(StringBuffer, 0, ByteChunk);
BytesReadTask.Wait();
BytesRead = BytesReadTask.Result;
}
return Task<object>.Run(() => BuilderToString(StringData));
}
private object BuilderToString(StringBuilder StringData)
{
return StringData.ToString();
}
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, System.Net.Http.HttpContent content, System.Net.TransportContext transportContext)
{
byte[] StringBuffer = StringEncoder.GetBytes((string)value);
return writeStream.WriteAsync(StringBuffer, 0, StringBuffer.Length);
}
}
}
Controller:
[HttpPost]
public async Task<HttpResponseMessage> UsingString([FromBody]string XmlAsString)
{
if (XmlAsString == null)
{
return this.Request.CreateResponse(HttpStatusCode.BadRequest);
}
return this.Request.CreateResponse(HttpStatusCode.OK, new { });
}
Setup nel metodo WebApiConfig.cs Register:
config.Formatters.Add(new TextMediaTypeFormmatter());
Fiddler intestazioni:
User-Agent: Fiddler
Content-Type: text/plain
fonte
2015-04-30 15:56:45
a cosa hai impostato l'intestazione Content-Type quando stavi cercando di inviare XML nel corpo? Potrebbe essere utile se hai aggiornato la tua domanda per mostrare tutto nella scheda Compositore che stai inviando. – EricLaw
trovato la risposta, avevo bisogno di un modo per attaccare un pezzo di XML nella coppia chiave/valore e l'utilizzo di ActionFilter sembra funzionare .. Ora ho solo bisogno di capire come analizzare l'XML in Classic ASP .. – punkouter