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