asp.net的WebAPI在webconfig加上MediaTypeFormatter [英] asp.net WebApi add MediaTypeFormatter in webconfig

查看:1205
本文介绍了asp.net的WebAPI在webconfig加上MediaTypeFormatter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 TestFormatter.cs延长MediaTypeFormatter 命名的自定义介质格式化 TestFormatter 。现在,我想添加这个格式。一种选择是:

I have a custom media formatter named TestFormatter in TestFormatter.cs extending MediaTypeFormatter. Now I want to add this formatter. One option is:

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



如果我想在web.config文件中添加此媒体类型格式。我见过

what if I want to add this media type formatter in web.config file. 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>



节点。但我不知道正确的语法来添加它。我曾尝试使用Google,但没有成功。

node in web.config file. But i do not know the right syntax to add it. I have tried googling but no success.

推荐答案

你正在尝试做不能通过仅使用配置文件设置完成;不幸的是,你需要编写一些代码来得到你想要的。这是因为web.config文件是ASP.NET / IIS结构和的WebAPI被设计成的自托管(IIS / ASP.NET外)。

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).

我建议添加您自己自定义的ConfigurationSection的配置文件。一个MediaTypeFormatters属性/元素添加到您的ConfigurationSection类,并有该属性是ConfigurationElementCollection中。然后,在你的网站的Global.asax中,添加一些代码,复制您的自定义集合中的任何MediaTypeFormatters到活收集连接到HttpConfiguration。这的链接包含使用类似的方法使用web.config设置来设置HttpConfiguration.IncludeErrorDetailPolicy属性的一个例子;而这不正是你正在尝试做它应该提供一个很好的起点。

I recommend adding your own custom ConfigurationSection to the config file. Add a MediaTypeFormatters property / element to your ConfigurationSection class, and have that property be a 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. This link contains an example of using a similar approach to set the HttpConfiguration.IncludeErrorDetailPolicy property using web.config settings; while it's not exactly what you are trying to do it should provide a great starting point.

这篇关于asp.net的WebAPI在webconfig加上MediaTypeFormatter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆