.Net Core 制作代理
.Net Core Making a Proxy
我正在尝试开发一个简单的代理,用于在我的第一个应用程序和第二个应用程序之间转发 api 请求。对于 Get 请求,它工作得很好,但我现在正尝试转发一个带有实际 body 的请求,但没有成功。我考虑过为此使用中间件,但我不认为使用中间件可以解决这个特定问题。
这是我的代理控制器:
[HttpGet("/api/corporations/{*url}")]
public async Task<string> GetCorporations(string url)
{
var result = await _httpClient.GetAsync("SomeCoolUrl");
Response.ContentType = "application/json";
return await result.Content.ReadAsStringAsync();
}
[HttpPatch("/api/corporations/{*url}")]
public async Task<string> PatchCorporations(string url, [FromBody]object body)
{
var result = await HttpClientExtension.PatchAsync(_httpClient, "SomeCoolUrl", new StringContent(body.ToString()));
Response.ContentType = "application/json";
return await result.Content.ReadAsStringAsync();
}
我这样实现了 PatchAsync(在 HttpClientExtension 中):
public static async Task<HttpResponseMessage> PatchAsync(HttpClient httpClient, string url, StringContent content)
{
return await httpClient.SendAsync(new HttpRequestMessage(new HttpMethod("PATCH"), url) { Content = content });
}
问题是我在发送补丁请求时收到不受支持的媒体类型,这无疑是因为 object.tostring() 不是有效的 json。但是我不知道要使用什么类型的 object 来正确保留补丁请求 body。如果我尝试 String 或 StringContent 而不是 object 它们总是空的。我想保持它的通用性,所以我无法像通常那样定义与特定 body 匹配的 object。
我正在使用 AspNetCore 1.1.2。
"Unsupported media type" 将是您的应用程序无法根据请求的内容类型将其 PatchCorporations 绑定到 it 的结果。使用 string body
而不是 object body
以便 ASP.NET 可以将 HTTP 请求与其匹配。通常,content negotiation will use content-type of "application/json" or "application/xml" (for example) to automatically serialize/de-serialize the HTTP request into the correct controller method via parameter binding.
确实是应该使用的对象。
问题是 new StringContent(body.ToString())
不是正确的请求负载。我将其替换为 new StringContent(content: JsonConvert.SerializeObject(body), encoding: Encoding.UTF8, mediaType: "application/json");
,现在可以正常使用了!
我正在尝试开发一个简单的代理,用于在我的第一个应用程序和第二个应用程序之间转发 api 请求。对于 Get 请求,它工作得很好,但我现在正尝试转发一个带有实际 body 的请求,但没有成功。我考虑过为此使用中间件,但我不认为使用中间件可以解决这个特定问题。
这是我的代理控制器:
[HttpGet("/api/corporations/{*url}")]
public async Task<string> GetCorporations(string url)
{
var result = await _httpClient.GetAsync("SomeCoolUrl");
Response.ContentType = "application/json";
return await result.Content.ReadAsStringAsync();
}
[HttpPatch("/api/corporations/{*url}")]
public async Task<string> PatchCorporations(string url, [FromBody]object body)
{
var result = await HttpClientExtension.PatchAsync(_httpClient, "SomeCoolUrl", new StringContent(body.ToString()));
Response.ContentType = "application/json";
return await result.Content.ReadAsStringAsync();
}
我这样实现了 PatchAsync(在 HttpClientExtension 中):
public static async Task<HttpResponseMessage> PatchAsync(HttpClient httpClient, string url, StringContent content)
{
return await httpClient.SendAsync(new HttpRequestMessage(new HttpMethod("PATCH"), url) { Content = content });
}
问题是我在发送补丁请求时收到不受支持的媒体类型,这无疑是因为 object.tostring() 不是有效的 json。但是我不知道要使用什么类型的 object 来正确保留补丁请求 body。如果我尝试 String 或 StringContent 而不是 object 它们总是空的。我想保持它的通用性,所以我无法像通常那样定义与特定 body 匹配的 object。
我正在使用 AspNetCore 1.1.2。
"Unsupported media type" 将是您的应用程序无法根据请求的内容类型将其 PatchCorporations 绑定到 it 的结果。使用 string body
而不是 object body
以便 ASP.NET 可以将 HTTP 请求与其匹配。通常,content negotiation will use content-type of "application/json" or "application/xml" (for example) to automatically serialize/de-serialize the HTTP request into the correct controller method via parameter binding.
确实是应该使用的对象。
问题是 new StringContent(body.ToString())
不是正确的请求负载。我将其替换为 new StringContent(content: JsonConvert.SerializeObject(body), encoding: Encoding.UTF8, mediaType: "application/json");
,现在可以正常使用了!