How to return XML from an ASP.NET 5 MVC 6 controller action

Domysee

How can I return XML from a controller action? Even when I add the header Accept: application/xml it returns a JSON object.

WebApi controllers in MVC 5 supported this. What do I have to do to make it work in MVC 6?

Domysee

Microsoft removed the XML formatter, so that ASP.NET MVC 6 returns only JSON by default. If you want to add support for XML again, call AddXmlSerializerFormatters after services.AddMvc() in your Startup.ConfigureServices() method:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()
        .AddXmlSerializerFormatters();
}

To use it, you have to add "Microsoft.AspNet.Mvc.Formatters.Xml": "6.0.0-rc1-final" as dependency (in the project.json under dependencies).


A slightly more tedious way of doing the same thing would be to add the Xml formatter directly to the OutputFormatters collection:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc(options =>
    {
        options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
    });
}

XmlSerializerOutputFormatter is in the namespace Microsoft.AspNet.Mvc.Formatters.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to unit test a Controller action using the Response property in ASP.NET 5 (MVC 6)?

From Dev

How to get access token in Asp.net MVC5 Controller action from OwinContext

From Dev

How to hide action and controller from url : asp.net mvc

From Dev

How to hide action and controller from url : asp.net mvc

From Dev

How to generate a view from a controller in VS 2015 ASP.NET 5 MVC6

From Dev

ASP.NET 5 - MVC 6 - Unit Test a Controller that uses Url.Action

From Dev

How to retrieve controller and action names from Html.Action that is rendered from different controller and action in ASP.NET MVC

From Dev

What is the best way to return File or ErrorMessage from Asp.net-mvc controller action?

From Dev

Return JSON from any ASP.NET MVC controller action based on a parameter

From Dev

ASP.NET MVC post to controller action from same controller

From Dev

how to define action link to action from controller in an area in asp.net mvc

From Dev

Cant get ASP.NET MVC 6 Controller to return JSON

From Dev

ASP.NET 5 MVC 6: Route to action by Accept header

From Dev

ASP.NET 5 / MVC 6 Ajax post Model to Controller

From Dev

How can i refresh two jqGrid's from the same asp.net-mvc controller action?

From Dev

How to pass an id from a razor view to a controller's action method in ASP .Net MVC?

From Dev

ASP.Net MVC How can I access hiddenfields from layout in controller action

From Dev

How can add controller in ASP.NET MVC6 beta5 project?

From Dev

In ASP.net 5 MVC 6 , How to use same controller name in different namespaces

From Dev

How can add controller in ASP.NET MVC6 beta5 project?

From Dev

Return action to action in asp.net mvc

From Dev

How to make ASP.Net MVC Controller Action Async

From Dev

How routing works with default controller and action in asp.net mvc

From Dev

Return Partial View and JSON from ASP.NET MVC Action

From Dev

How to return xml or json with ASP.NET web mvc 6 depending on Accept Header

From Dev

Return Pdf Docs from Controller in ASP.NET Core MVC

From Dev

ASP.Net 5 MVC 6 - how to return invalid JSON message on POST using FromBody?

From Dev

How do I return a JSON object from controller in ASP.NET MVC 4?

From Dev

How to return JSON in a ASP.NET MVC/web controller?

Related Related

  1. 1

    How to unit test a Controller action using the Response property in ASP.NET 5 (MVC 6)?

  2. 2

    How to get access token in Asp.net MVC5 Controller action from OwinContext

  3. 3

    How to hide action and controller from url : asp.net mvc

  4. 4

    How to hide action and controller from url : asp.net mvc

  5. 5

    How to generate a view from a controller in VS 2015 ASP.NET 5 MVC6

  6. 6

    ASP.NET 5 - MVC 6 - Unit Test a Controller that uses Url.Action

  7. 7

    How to retrieve controller and action names from Html.Action that is rendered from different controller and action in ASP.NET MVC

  8. 8

    What is the best way to return File or ErrorMessage from Asp.net-mvc controller action?

  9. 9

    Return JSON from any ASP.NET MVC controller action based on a parameter

  10. 10

    ASP.NET MVC post to controller action from same controller

  11. 11

    how to define action link to action from controller in an area in asp.net mvc

  12. 12

    Cant get ASP.NET MVC 6 Controller to return JSON

  13. 13

    ASP.NET 5 MVC 6: Route to action by Accept header

  14. 14

    ASP.NET 5 / MVC 6 Ajax post Model to Controller

  15. 15

    How can i refresh two jqGrid's from the same asp.net-mvc controller action?

  16. 16

    How to pass an id from a razor view to a controller's action method in ASP .Net MVC?

  17. 17

    ASP.Net MVC How can I access hiddenfields from layout in controller action

  18. 18

    How can add controller in ASP.NET MVC6 beta5 project?

  19. 19

    In ASP.net 5 MVC 6 , How to use same controller name in different namespaces

  20. 20

    How can add controller in ASP.NET MVC6 beta5 project?

  21. 21

    Return action to action in asp.net mvc

  22. 22

    How to make ASP.Net MVC Controller Action Async

  23. 23

    How routing works with default controller and action in asp.net mvc

  24. 24

    Return Partial View and JSON from ASP.NET MVC Action

  25. 25

    How to return xml or json with ASP.NET web mvc 6 depending on Accept Header

  26. 26

    Return Pdf Docs from Controller in ASP.NET Core MVC

  27. 27

    ASP.Net 5 MVC 6 - how to return invalid JSON message on POST using FromBody?

  28. 28

    How do I return a JSON object from controller in ASP.NET MVC 4?

  29. 29

    How to return JSON in a ASP.NET MVC/web controller?

HotTag

Archive