Return xml with 406, Net Core 2.2, with Formatfilter?

Asked

Viewed 21 times

0

I’m trying to make a comeback in mine Controller with FormatFilter .xml, but I get the 406.

Controller:

[HttpGet("xml/loja/{id}/{format}"), FormatFilter]
[Produces("application/xml")]
[AllowAnonymous]
public async Task<ActionResult<filterResultSet>> GetXmlLoja(
    [FromHeader] Guid userId, 
    [FromHeader] string fileName, 
    Guid id
) 
{ 
    //FormatFilterAttribute format = FormatFilterAttribute.GetCustomAttribute(XmlDocument)
    //if (!_mapaRelatorioBO.Exists(id).Result) return NotFound(new { message = "MapaRelatorio não existente!" });
    //if (userId == null) return BadRequest(new { message = "Usuário não informado!" });
    return await _mapaRelatorioBO.GetXmlLoja(id, fileName);
}

Startup.Cs

services.AddMvcCore(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"));
})
.AddFormatterMappings();

services.AddMvc(options =>
{
    options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
});

Return

inserir a descrição da imagem aqui

The format of the submission is: filterResultSet filter = new filterResultSet();

namespace GI.NET.Insights.Dominio.Xml
{
    public class EstruturaLojaXml
    {


    }


    // NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http", IsNullable = false)]
    public partial class filterResultSet
    {

        private filterResultSetFilterResult filterResultField;

        /// <remarks/>
        public filterResultSetFilterResult filterResult
        {
            get
            {
                return this.filterResultField;
            }
            set
            {
                this.filterResultField = value;
            }
        }
    } ...

by the result that should be successful the Swagger shows me so:

inserir a descrição da imagem aqui

  • How are you sending?

  • I asked the question. :)

  • It seems to me that your request does not accept to receive an xml response

  • Example Value is the example that should be sent, if you are sending this xml?

  • In Swagger, after clicking on Try out just below the execute button on the right has a select "Response content type", tried to change to "application/xml"?

  • is has several aspects equal @Leandroangelo quoted for that all this works...

  • @Leandroangelo has already changed, when I run the project. I put his image complete.

  • @novic the Structurelojaxml class mounts the full XML object, as generated by the visual studio itself. from the filterResult object. He’s too big, so I didn’t put it all.

  • get? method are enough problems that I’ve encountered, what you want to do basically?

  • Another application will call mine, and I have to return an xml to it. Practically this.

  • When I call my application, it reads all of it. But, on the 406 return. Something from Header. I even found a possible solution, but it only works for the core >3.0 https://docs.microsoft.com/pt-br/aspnet/core/web-api/advanced/formatting?view=aspnetcore-5.0

  • how is it calling?

  • For now, by the same Swagger, inserting the id, and the format.

  • And also did not understand, why the filter if you have a unique route to return xml

  • @Leandroangelo, are attempts to make it work, none worked. srsrsrsrsr I managed to make it work with core 3.0. but with this not yet.

  • So I have an example with the latest version v5 and works perfectly do not need to write anything in the class just confiugrar and use and even print xml or json

  • I checked my class. E some System.Xml.Serialization were not working. Hence error 406. It was necessary to redo them. (were automatically generated by Visual Studio from an . xml file)

Show 12 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.