[英]asp.net WebApi add MediaTypeFormatter in webconfig
I have a custom media formatter named TestFormatter
in TestFormatter.cs extending MediaTypeFormatter
.我在
TestFormatter.cs extending MediaTypeFormatter
TestFormatter
中有一个名为TestFormatter
的自定义媒体格式化程序。 Now I want to add this formatter.现在我想添加这个格式化程序。 One option is:
一种选择是:
config.Formatters.Add(new BinaryMediaTypeFormatter());
what if I want to add this media type formatter in web.config file.如果我想在 web.config 文件中添加这个媒体类型格式化程序怎么办。 I have seen
我见过
<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>
node in web.config file. web.config 文件中的节点。 But i do not know the right syntax to add it.
但我不知道添加它的正确语法。 I have tried googling but no success.
我试过谷歌搜索但没有成功。
What you are trying to do cannot be done through the use of config file settings only;您尝试做的事情不能仅通过使用配置文件设置来完成; unfortunately, you will need to write some code to get where you want to be.
不幸的是,您需要编写一些代码才能到达您想要的位置。 This is because the web.config file is an ASP.NET / IIS construct and WebAPI was designed to be self-hosted (outside of IIS / ASP.NET).
这是因为 web.config 文件是 ASP.NET / IIS 构造,并且 WebAPI 被设计为自托管(在 IIS / ASP.NET 之外)。
I recommend adding your own custom ConfigurationSection to the config file.我建议将您自己的自定义 ConfigurationSection 添加到配置文件中。 Add a MediaTypeFormatters property / element to your ConfigurationSection class, and have that property be a ConfigurationElementCollection.
将 MediaTypeFormatters 属性/元素添加到您的 ConfigurationSection 类,并将该属性设为 ConfigurationElementCollection。 Then, in your website's Global.asax, add some code that copies any MediaTypeFormatters in your custom collection over to the "live" collection attached to HttpConfiguration.
然后,在您网站的 Global.asax 中,添加一些代码,将自定义集合中的任何 MediaTypeFormatters 复制到附加到 HttpConfiguration 的“实时”集合。 This link contains an example of using a similar approach to set the HttpConfiguration.IncludeErrorDetailPolicy property using web.config settings;
此链接包含使用类似方法使用 web.config 设置设置 HttpConfiguration.IncludeErrorDetailPolicy 属性的示例; while it's not exactly what you are trying to do it should provide a great starting point.
虽然这不是你想要做的,但它应该提供一个很好的起点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.