c# - 找不到方法 : 'System.Net.Http.Headers.MediaTypeHeaderValue System.Net.Http.Formatting.JsonMediaTypeFormatter.get_DefaultMediaType()'

标签 c# asp.net asp.net-mvc-4 asp.net-web-api .net-4.5

我有一个名为 GetJSONResponse(...) 的方法,这是一个摘录:

var response = new HttpResponseMessage(httpResponseCode)
            {
                Content = objectToSerialize != null ?
                    new ObjectContent(objectToSerialize.GetType(), objectToSerialize, new JsonMediaTypeFormatter(), JsonMediaTypeFormatter.DefaultMediaType.MediaType)
                    : null
            };

在调用此方法之前一切正常,然后我在输入方法之前收到此错误(也许 ASP.NET 根据需要编译方法?):

Method not found: 'System.Net.Http.Headers.MediaTypeHeaderValue System.Net.Http.Formatting.JsonMediaTypeFormatter.get_DefaultMediaType()'.

最初我认为 web.config 文件(用于 View )中缺少 namespace ,我试图添加它,但它没有解决任何问题 - 然后我意识到我实际上是通过 WebAPI 调用它 Controller “获取”方法(不是 View )。所需的 DLL 存在(复制到本地是正确的),但这似乎仍然失败。很明显错误意味着找不到方法,但它编译和启动正常,因为 c# 文件正确引用类型没有问题......有什么想法吗?

最佳答案

发现问题:我在主 web.config 文件中有一个重复的依赖引用:

错一个:

<dependentAssembly>
    <assemblyIdentity name="System.Net.Http"
        publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.2.29.0" newVersion="2.2.29.0" />
</dependentAssembly>

右一:

<dependentAssembly>
    <assemblyIdentity name="System.Net.Http"
        publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>

正确的在列表的后面很多,所以我没有注意到它是重复的。 ;)

... 想想看,这也是 API Help Page 的原因有一个问题。 ApiDescriptionExtensions.GetFriendlyId() 也由于此问题而无法编译和运行,但现在可以使用了。 ;) 导致此错误:找不到方法:'System.Net.Http.HttpMethod System.Web.Http.Description.ApiDescription.get_HttpMethod()'。

关于c# - 找不到方法 : 'System.Net.Http.Headers.MediaTypeHeaderValue System.Net.Http.Formatting.JsonMediaTypeFormatter.get_DefaultMediaType()' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36853647/

相关文章:

C#:具有相同内容的字符串

c# - 在 JS 方法中获取变量的内容 asp.net C#

asp.net-mvc-4 - JQuery 验证全局化 MVC 4 DateTime

asp.net-mvc-3 - MVC4 Razor 中可能存在重大变化,可通过 "@:@"修复

ASP.NET MVC4 与 Twitter Bootstrap 捆绑

c# - 在 asp.net core 中访问被拒绝的文件

c# - Android 手机作为 PC 中的游戏 Controller

c# - 阅读句子以制作字典或数组或关键字/关键短语列表

c# - 如何手动定位 ASP.Net Chart Legend?

javascript - 从代码隐藏调用javascript方法