Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OutputFormatter XmlDataContractSerializerOutputFormatter asp.net核心未到达子级的子级_Asp.net_Xml_Api_Asp.net Core Webapi_Core - Fatal编程技术网

OutputFormatter XmlDataContractSerializerOutputFormatter asp.net核心未到达子级的子级

OutputFormatter XmlDataContractSerializerOutputFormatter asp.net核心未到达子级的子级,asp.net,xml,api,asp.net-core-webapi,core,Asp.net,Xml,Api,Asp.net Core Webapi,Core,我的.Net Core API测试项目包含应以xml格式返回的数据,因此我在Startup类中安装并设置了XmlDataContractSerializerOutputFormatter services.AddMvc(setupAction => { setupAction.ReturnHttpNotAcceptable = true; setupAction.OutputFormatters.Ad

我的.Net Core API测试项目包含应以xml格式返回的数据,因此我在Startup类中安装并设置了XmlDataContractSerializerOutputFormatter

services.AddMvc(setupAction => 
            {
                setupAction.ReturnHttpNotAcceptable = true;
                setupAction.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter()); 
            });
一旦我将Postman中的Accept头设置为application/xml值,数据就会以xml的形式返回,但这只能到达父级和第一个子级,之后,结果是标准的JSON格式。

控制器代码:

namespace Lib.Controllers
{
    [Route("api/authors/{authorId}/books")]
    public class BooksController : Controller
    {
        private ILibraryRepository _libraryRepository;

        public BooksController(ILibraryRepository libraryRepository)
        {
            _libraryRepository = libraryRepository;
        }

        [HttpGet()]
        public IActionResult GetBooksForAuthor(Guid authorId)
        {
            if (!_libraryRepository.AuthorExists(authorId))
            {
                return NotFound();
            }

            var booksForAuthorFromRepo = _libraryRepository.GetBooksForAuthor(authorId);

            var booksForAuthor = AutoMapper.Mapper.Map<IEnumerable<BookDto>>(booksForAuthorFromRepo);

            return Ok(booksForAuthor);
        }

        [HttpGet("{id}")]
        public IActionResult GetBookForAuthor(Guid authorId, Guid Id)
        {
            if (!_libraryRepository.AuthorExists(authorId))
            {
                return NotFound();
            }
            var bookForAuthorFromRepo = _libraryRepository.GetBookForAuthor(authorId, Id);
            if (bookForAuthorFromRepo == null)
            {
                return NotFound("The book does not exist");
            }
            var bookForAuthor = AutoMapper.Mapper.Map<BookDto>(bookForAuthorFromRepo);
            return new JsonResult(bookForAuthor);
        }
    }
}
namespace Lib.Controllers
{
    [Route("api/authors/{authorId}/books")]
    public class BooksController : Controller
    {
        private ILibraryRepository _libraryRepository;

        public BooksController(ILibraryRepository libraryRepository)
        {
            _libraryRepository = libraryRepository;
        }

        [HttpGet()]
        public IActionResult GetBooksForAuthor(Guid authorId)
        {
            if (!_libraryRepository.AuthorExists(authorId))
            {
                return NotFound();
            }

            var booksForAuthorFromRepo = _libraryRepository.GetBooksForAuthor(authorId);

            var booksForAuthor = AutoMapper.Mapper.Map<IEnumerable<BookDto>>(booksForAuthorFromRepo);

            return Ok(booksForAuthor);
        }

        [HttpGet("{id}")]
        public IActionResult GetBookForAuthor(Guid authorId, Guid Id)
        {
            if (!_libraryRepository.AuthorExists(authorId))
            {
                return NotFound();
            }
            var bookForAuthorFromRepo = _libraryRepository.GetBookForAuthor(authorId, Id);
            if (bookForAuthorFromRepo == null)
            {
                return NotFound("The book does not exist");
            }
            var bookForAuthor = AutoMapper.Mapper.Map<BookDto>(bookForAuthorFromRepo);
            return new JsonResult(bookForAuthor);
        }
    }
}
名称空间库控制器
{
[路径(“api/authors/{authorId}/books”)]
公共类控制器:控制器
{
私有ILibraryRepository(ILibraryRepository);
公共图书管理员(ILibraryRepository libraryRepository)
{
_libraryRepository=libraryRepository;
}
[HttpGet()]
公共IActionResult GetBooksForAuthor(Guid authorId)
{
if(!\u libraryRepository.AuthorExists(authorId))
{
返回NotFound();
}
var booksForAuthorFromRepo=\u libraryRepository.GetBooksForAuthor(authord);
var booksForAuthor=AutoMapper.Mapper.Map(booksForAuthorFromRepo);
返回Ok(booksForAuthor);
}
[HttpGet(“{id}”)]
public IActionResult GetBookForAuthor(Guid authorId,Guid Id)
{
if(!\u libraryRepository.AuthorExists(authorId))
{
返回NotFound();
}
var bookForAuthorFromRepo=\u libraryRepository.GetBookForAuthor(authorId,Id);
if(bookForAuthorFromRepo==null)
{
return NotFound(“该书不存在”);
}
var bookForAuthor=AutoMapper.Mapper.Map(bookForAuthorFromRepo);
返回新的JsonResult(bookForAuthor);
}
}
}
图书管理员代码:

namespace Lib.Controllers
{
    [Route("api/authors/{authorId}/books")]
    public class BooksController : Controller
    {
        private ILibraryRepository _libraryRepository;

        public BooksController(ILibraryRepository libraryRepository)
        {
            _libraryRepository = libraryRepository;
        }

        [HttpGet()]
        public IActionResult GetBooksForAuthor(Guid authorId)
        {
            if (!_libraryRepository.AuthorExists(authorId))
            {
                return NotFound();
            }

            var booksForAuthorFromRepo = _libraryRepository.GetBooksForAuthor(authorId);

            var booksForAuthor = AutoMapper.Mapper.Map<IEnumerable<BookDto>>(booksForAuthorFromRepo);

            return Ok(booksForAuthor);
        }

        [HttpGet("{id}")]
        public IActionResult GetBookForAuthor(Guid authorId, Guid Id)
        {
            if (!_libraryRepository.AuthorExists(authorId))
            {
                return NotFound();
            }
            var bookForAuthorFromRepo = _libraryRepository.GetBookForAuthor(authorId, Id);
            if (bookForAuthorFromRepo == null)
            {
                return NotFound("The book does not exist");
            }
            var bookForAuthor = AutoMapper.Mapper.Map<BookDto>(bookForAuthorFromRepo);
            return new JsonResult(bookForAuthor);
        }
    }
}
namespace Lib.Controllers
{
    [Route("api/authors/{authorId}/books")]
    public class BooksController : Controller
    {
        private ILibraryRepository _libraryRepository;

        public BooksController(ILibraryRepository libraryRepository)
        {
            _libraryRepository = libraryRepository;
        }

        [HttpGet()]
        public IActionResult GetBooksForAuthor(Guid authorId)
        {
            if (!_libraryRepository.AuthorExists(authorId))
            {
                return NotFound();
            }

            var booksForAuthorFromRepo = _libraryRepository.GetBooksForAuthor(authorId);

            var booksForAuthor = AutoMapper.Mapper.Map<IEnumerable<BookDto>>(booksForAuthorFromRepo);

            return Ok(booksForAuthor);
        }

        [HttpGet("{id}")]
        public IActionResult GetBookForAuthor(Guid authorId, Guid Id)
        {
            if (!_libraryRepository.AuthorExists(authorId))
            {
                return NotFound();
            }
            var bookForAuthorFromRepo = _libraryRepository.GetBookForAuthor(authorId, Id);
            if (bookForAuthorFromRepo == null)
            {
                return NotFound("The book does not exist");
            }
            var bookForAuthor = AutoMapper.Mapper.Map<BookDto>(bookForAuthorFromRepo);
            return new JsonResult(bookForAuthor);
        }
    }
}
名称空间库控制器
{
[路径(“api/authors/{authorId}/books”)]
公共类控制器:控制器
{
私有ILibraryRepository(ILibraryRepository);
公共图书管理员(ILibraryRepository libraryRepository)
{
_libraryRepository=libraryRepository;
}
[HttpGet()]
公共IActionResult GetBooksForAuthor(Guid authorId)
{
if(!\u libraryRepository.AuthorExists(authorId))
{
返回NotFound();
}
var booksForAuthorFromRepo=\u libraryRepository.GetBooksForAuthor(authord);
var booksForAuthor=AutoMapper.Mapper.Map(booksForAuthorFromRepo);
返回Ok(booksForAuthor);
}
[HttpGet(“{id}”)]
public IActionResult GetBookForAuthor(Guid authorId,Guid Id)
{
if(!\u libraryRepository.AuthorExists(authorId))
{
返回NotFound();
}
var bookForAuthorFromRepo=\u libraryRepository.GetBookForAuthor(authorId,Id);
if(bookForAuthorFromRepo==null)
{
return NotFound(“该书不存在”);
}
var bookForAuthor=AutoMapper.Mapper.Map(bookForAuthorFromRepo);
返回新的JsonResult(bookForAuthor);
}
}
}

将返回类型从JsonResult()更改为Ok()解决了问题。
感谢@PanagiotisKanavos

发布相关代码-控制器、操作以及创建和返回响应数据的代码。屏幕截图没有显示与问题相关的任何内容-它们显示了两个不同的请求,而不是一个包含子项的XML文档。原始响应是什么?@PanagiotisKanavos这是原始响应
{“id”:“c7ba6add-09c4-45f8-8dd0-eaca221e5d93”,“title”:“The Shining”,“description”:“光辉是美国作家斯蒂芬·金(Stephen King)的一部恐怖小说。1977年出版,这是金的第三部出版的小说,也是第一本精装本畅销书:这本书的成功使金成为恐怖流派中杰出的作家。”,“authorId”:“25320c5e-f58a-4b1f-b63a-8ee07a840bdf”
返回新的JsonResult
?@PanagiotisKanavos谢谢,将返回结果更改为
返回Ok
解决了问题。