嗨,我有3天的时间,但还活着,
这是我在MVC核心中的代码,它使用mvc API Core api项目在本地工作并且由邮递员测试
string Baseurl = "http://api.site.com";
// GET: CustomerTypes
public async Task<IActionResult> Index()
{
List<CustomerType> empInfo = new List<CustomerType>();
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(Baseurl);
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Add("Connection", "Keep-Alive");
client.DefaultRequestHeaders.Add("Keep-Alive", "3600");
//Define request data format
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//Sending request to find web api REST service resource GetAllEmployees using HttpClient
HttpResponseMessage res = await client.GetAsync("/api/customerTypes");
if (res.IsSuccessStatusCode)
{
//Storing the response details recieved from web api
var empResponse = res.Content.ReadAsStringAsync().Result;
//Deserializing the response recieved from web api and storing into the Employee list
empInfo = JsonConvert.DeserializeObject<List<CustomerType>>(empResponse);
}
//returning the employee list to view
return View(empInfo);
}
这是在mvc api项目的startup.cs中配置
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigin",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
);
});}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// app.UseCorsMiddleware();
// Shows UseCors with named policy.
app.UseCors("AllowSpecificOrigin");
app.UseMvc();
}
控制API设置[EnableCors(&#34; AllowSpecificOrigin&#34;)]每个控件
我尝试了很多方法: 1-从本地运行api并通过远程网络呼叫
2-在远程发布api,从本地发布cal
3-通过邮递员远程站点呼叫远程api
所有这一切都不起作用,只有在邮寄人工作的情况下调用本地api
处理请求时发生未处理的异常。
SocketException:远程主机强行关闭现有连接 System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
IOException:无法从传输连接读取数据:远程主机强行关闭现有连接。 System.Net.ConnectStream.EndRead(IAsyncResult asyncResult)
HttpRequestException:将内容复制到流时出错。 System.Net.Http.HttpContent + d__49.MoveNext()
答案 0 :(得分:0)
对于我的案例,我与主机管理员联系并重置IIS,它对我有用。