简体   繁体   English

asp.net WebApi 在 webconfig 中添加 MediaTypeFormatter

[英]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.

相关问题 ASP.Net WebAPI从MediaTypeFormatter内部获取当前控制器名称 - ASP.Net WebAPI Get current controller name from inside MediaTypeFormatter 没有 MediaTypeFormatter 可用于从 asp.net mvc webapi 中媒体类型为“text/html”的内容中读取“IEnumerable`1”类型的对象 - No MediaTypeFormatter is available to read an object of type 'IEnumerable`1' from content with media type 'text/html in asp.net mvc webapi 将身份验证添加到ASP.NET WebApi 2.2 - Add Authentication to ASP.NET WebApi 2.2 ASP.NET MVC - 从 webConfig 更改 appsettings 标记位置 - ASP.NET MVC - Change appsettings tag location from webConfig 用于身份验证或登录所需Web服务器的asp.net webconfig配置 - asp.net webconfig configuration for authentication or login required webserver ASP.NET MVC配置另一个项目webconfig - ASP.NET MVC Configure the another project webconfig 部署ASP.NET网站时WebConfig文件出现问题 - Issues with WebConfig file while deploying ASP.NET website 使用asp.net在webconfig中放大上传的文件大小 - enlarge uploaded file size in webconfig using asp.net 如何向ASP.NET WebAPI控制器添加自定义方法? - How to add custom methods to ASP.NET WebAPI controller? ASP.net WebAPI将标头添加到所有出站请求 - ASP.net webapi add header to all outbound requests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM