Ich möchte eine Webanwendung erstellen, die Daten in Form von XML oder JSON zurückgibt, wie gehe ich dabei vor?MVC .NET Core Web API XML oder JSON
Das Modell:
namespace ReturningJSONandXML.Models
{
public class SomeImportantInformation
{
public int ID { get; set; }
public string Information { get; set; }
}
}
Der Controller:
namespace ReturningJSONandXML.Controllers
{
public class GetInfoController : Controller
{
// GET: /<controller>/
public List<SomeImportantInformation> Get()
{
List<SomeImportantInformation> ImportantInfo = new List<SomeImportantInformation>();
ImportantInfo.Add(new SomeImportantInformation { ID = 0, Information = "Awesome info" });
ImportantInfo.Add(new SomeImportantInformation { ID = 1, Information = "Some other interesting info" });
return ImportantInfo;
}
}
}
Ich möchte eine XML und JSON-Datei zurückzukehren ...
Was ist die beste Praxis der I verwendet werden sollte, Hier?
t er api wird die Antwort auf das konvertieren, was auch immer Sie auf Ihre Anfrage header ... application/json oder application/xml setzen – NicoRiff
Wenn Sie MVC 6 (ASP.NET Core) verwenden, wird das Framework automatisch den Inhalt aushandeln (den entsprechenden Inhalt zurücksenden). Typ wie @NicoRiff sagte). Weitere Informationen [in der Dokumentation] (https://docs.microsoft.com/en-us/aspnet/core/mvc/models/formatting#content-newtiation). –