XMLまたはJSONの形式でデータを返すWebアプリケーションを作成したいのですが、どうすればよいですか?ベストプラクティスの私が使用する必要がありますされている何MVC .NET Core Web API XMLまたはJSON
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;
}
}
}
私はXMLとJSONファイルを返したいと思います...
:
モデル:
namespace ReturningJSONandXML.Models
{
public class SomeImportantInformation
{
public int ID { get; set; }
public string Information { get; set; }
}
}
コントローラここに?
t彼はあなたのリクエストヘッダーに置いたものに応答を変換します... application/jsonまたはapplication/xml – NicoRiff
MVC 6(ASP.NET Core)を使用している場合、フレームワークは自動的にコンテンツをネゴシエートします(適切なcontent- @NicoRiffはこう言った)。詳細は[ドキュメントの中の](https://docs.microsoft.com/en-us/aspnet/core/mvc/models/formatting#content-negotiation)を参照してください。 –