gpt4 book ai didi

c# - asp.net Web Api 在网络配置中添加 MediaTypeFormatter

转载 作者:太空宇宙 更新时间:2023-11-03 12:59:22 24 4
gpt4 key购买 nike

我在 TestFormatter.cs extending MediaTypeFormatter 中有一个名为 TestFormatter 的自定义媒体格式化程序。现在我想添加这个格式化程序。一种选择是:

config.Formatters.Add(new BinaryMediaTypeFormatter());

如果我想在 web.config 文件中添加此媒体类型格式化程序怎么办。我见过

<webApi.webServer>
<services>
<replace serviceType="System.Web.Http.ExceptionHandling.IExceptionHandler, System.Web.Http" serviceClass="Slb.Avocet.WebApiServer.ExceptionHandling.GlobalExceptionHandler, Slb.Avocet.WebApiServer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<replace serviceType="System.Web.Http.Description.IDocumentationProvider, System.Web.Http" serviceClass="Slb.Avocet.WebApiServer.Documentation.XmlDocumentationProvider, Slb.Avocet.WebApiServer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</services>
<mediaTypeFormatters>
</mediaTypeFormatters>
<messageHandlers>
</messageHandlers>
</webApi.webServer>

web.config 文件中的节点。但我不知道添加它的正确语法。我试过谷歌搜索但没有成功。

最佳答案

您尝试做的事情不能仅通过使用配置文件设置来完成;不幸的是,您需要编写一些代码才能到达您想要的位置。这是因为 web.config 文件是一个 ASP.NET/IIS 结构,而 WebAPI 被设计为 self-hosted。 (在 IIS/ASP.NET 之外)。

我建议将您自己的自定义 ConfigurationSection 添加到配置文件中。将 MediaTypeFormatters 属性/元素添加到您的 ConfigurationSection 类,并使该属性成为 ConfigurationElementCollection。然后,在您网站的 Global.asax 中,添加一些代码,将自定义集合中的任何 MediaTypeFormatters 复制到附加到 HttpConfiguration 的“实时”集合中。这link包含使用类似方法使用 web.config 设置设置 HttpConfiguration.IncludeErrorDetailPolicy 属性的示例;虽然这不完全是您要尝试做的事情,但它应该提供一个很好的起点。

关于c# - asp.net Web Api 在网络配置中添加 MediaTypeFormatter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32831029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com