0

I have a project written in .NET and I updated my server from IIS 2012 to IIS 2019.

Before this update, every time I asked for response from the API and wrote &format=xml at the end of the URL I got the response in XML instead of Json.

For example:

https:....../get-customer-details?customerID=127836
my response in Json

https:....../get-customer-details?customerID=127836&format=xml
my response in XML

So now, after the update, &format=xml returns an error page can’t be found instead of XML response like before.

What may cause the problem?

Thanks!

I tried to check IIS configuration options and didn't find anything.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • The following may be of interest: https://github.com/rundeck/rundeck/issues/1957#issuecomment-237031293, https://stackoverflow.com/questions/9847564/how-do-i-get-asp-net-web-api-to-return-json-instead-of-xml-using-chrome, https://github.com/aspnet/Mvc/issues/1765, and https://andrewlock.net/formatting-response-data-as-xml-or-json-based-on-the-url-in-asp-net-core/ – Tu deschizi eu inchid Mar 16 '23 at 16:31
  • Did you upgrade from HTTP to HTTPS (secure). You may be having issues with the TLS used for a secure connection. IIS 2012 had limited HTTPS capability with limited encryption modes using TLS 1.2. A IIS 2019 is probably using TLS 1.3 with newer certificates and more advance encryption modes. TLS 1.3 in some cases will not work with old encryption modes. You probably need newer certificates to get TLS 1.3 to work. – jdweng Mar 17 '23 at 04:33
  • At least, you should read IIS logs to see what's the actual response status code for such requests. – Lex Li Mar 17 '23 at 04:47
  • Are your server configuration options the same as before? You can try to recycle the application pool and try again. – samwu Mar 17 '23 at 07:06

1 Answers1

0

Finally I get the solution by add this code to my startup.cs:

            services.AddMvc()
                .AddXmlSerializerFormatters();

        services.AddMvc(options =>
        {
            options.FormatterMappings.SetMediaTypeMappingForFormat
                ("xml", MediaTypeHeaderValue.Parse("application/xml"));
            options.FormatterMappings.SetMediaTypeMappingForFormat
                ("config", MediaTypeHeaderValue.Parse("application/xml"));
            options.FormatterMappings.SetMediaTypeMappingForFormat
                ("js", MediaTypeHeaderValue.Parse("application/json"));
        })
                .AddXmlSerializerFormatters();

Thank you all!

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 25 '23 at 00:13