简体   繁体   English

ASP.Net核心API访问HttpRequestMessage

[英]ASP.Net Core API Accessing HttpRequestMessage

I have a ASP.Net 4.5 application that I am trying to upgrade to ASP.Net Core. 我有一个ASP.Net 4.5应用程序,我正在尝试升级到ASP.Net Core。 This application receives calls from a 3rd party application. 此应用程序接收来自第三方应用程序的呼叫。

In my old application I have an action that looks like this: 在我的旧应用程序中,我有一个看起来像这样的动作:

public async Task<HttpResponseMessage> RealTimeAsync(HttpRequestMessage request)
{
    var StatusMessage = string.Empty;
    try
    {
        var doc = new XmlDocument();
        doc.Load(await request.Content.ReadAsStreamAsync());

From 4.5 this works fine. 从4.5这个工作正常。 However, when I use this code in ASP.Net core I get an "Object Reference Not Set to an Instance of an Object" error because request.Content is null. 但是,当我在ASP.Net核心中使用此代码时,我得到“对象引用未设置为对象的实例”错误,因为request.Content为null。

The requests coming in to the two applications (4.5 and .Net Core) are the same. 进入两个应用程序(4.5和.Net Core)的请求是相同的。 Why is request.Content null in my .Net Core application? 为什么我的.Net Core应用程序中的request.Content为null?

When I referenced this post: ASP.NET Core HTTPRequestMessage returns strange JSON message 当我引用这篇文章时: ASP.NET Core HTTPRequestMessage返回奇怪的JSON消息

I tried installing the suggested Nuget Package. 我尝试安装建议的Nuget包。 However, it is not compatible with .Net Core: 但是,它与.Net Core不兼容:

error: Package Microsoft.AspNet.WebApi.Client 5.2.2 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). 错误:包Microsoft.AspNet.WebApi.Client 5.2.2与netcoreapp1.0(.NETCoreApp,Version = v1.0)不兼容。 Package Microsoft.AspNet.WebApi.Client 5.2.2 supports: error: - net45 (.NETFramework,Version=v4.5) error: - portable-net45+netcore45+wp8+wp81+wpa81 (.NETPortable,Version=v0.0,Profile=wp8+netcore45+net45+wp81+‌​wpa81) error: One or more packages are incompatible with .NETCoreApp,Version=v1.0. 软件包Microsoft.AspNet.WebApi.Client 5.2.2支持:错误: - net45(.NETFramework,Version = v4.5)错误: - portable-net45 + netcore45 + wp8 + wp81 + wpa81(.NETPortable,Version = v0.0 ,Profile = wp8 + netcore45 + net45 + wp81 + wpa81)错误:一个或多个软件包与.NETCoreApp,Version = v1.0不兼容。

That code needs to be refactored to use more recent structure. 该代码需要重构才能使用更新的结构。

public Task<IActionResult> RealTimeAsync() {
    var StatusMessage = string.Empty;
    try {
        var request = this.Request;
        var doc = new XmlDocument();
        doc.Load(request.Body); //request.Body returns a stream
    //...other code...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 与HttpRequestMessage等效的ASP.NET Core是什么? - What is the ASP.NET Core equivalent to HttpRequestMessage? ASP.NET Core HTTPRequestMessage返回奇怪的JSON消息 - ASP.NET Core HTTPRequestMessage returns strange JSON message 在ASP.NET Core中未在HttpRequestMessage和HttpResponseMessage上调用Dispose - Not calling Dispose on HttpRequestMessage and HttpResponseMessage in asp.net core ASP.Net核心传递HttpRequestMessage参数始终为空 - ASP.Net Core Passing HttpRequestMessage Parameter always empty ASP.Net Web API Swashbuckle如何忽略HttpRequestMessage - ASP.Net Web API Swashbuckle how to ignore HttpRequestMessage 在 ASP.NET 内核 Web API 中访问和使用 UserClaims - Accessing and using UserClaims in ASP.NET Core Web API 访问配置作为服务 asp.net 核心 - Accessing Configuration as service asp.net core 在ASP.Net Web API中的日志记录DelegatingHandler中读取HttpRequestMessage.Content时丢失 - HttpRequestMessage.Content is lost when it is read in a logging DelegatingHandler in ASP.Net Web API 如何检查用户是否重新登录并访问 ASP.NET Core Web API 中的相同 API - How to check if a user re-logged in and accessing the same API in ASP.NET Core Web API 从 React 应用程序访问 asp.net 核心 api(均已容器化) - Accessing asp.net core api from react app (both containerized)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM