问题描述
我添加了行 .UseUrls("http://*:5000")
以启用来自其他主机的客户端访问 web api.
public static void Main(string[] args){var host = new WebHostBuilder().UseKestrel().UseContentRoot(Directory.GetCurrentDirectory()).UseIISIntegration().UseStartup().UseUrls("http://*:5000")//添加.建造();主机.运行();}
但是,使用浏览器访问localhost:5000/api/Test
却得到HTTP/1.1 400 Bad Request
的错误?.UseUrls()
是否应该只编译用于生产?
以下消息是在测试时从 Visual Studio 输出窗口复制的.
Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求开始 HTTP/1.1 GET http://localhost:5000/api/测试
Microsoft.AspNetCore.Server.IISIntegration.IISMiddleware:错误:MS-ASPNETCORE-TOKEN"与预期的配对令牌9bca37f2-7eda-4517-9f8f-60b6cc05cf01"不匹配,请求被拒绝.
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: 请求在 8.5976ms 400 内完成
你应该先调用 .UseUrls()
和/或 .UseConfig()
然后.UseIISIntegration()
.
当在 IIS/IISExpress 下运行正常时,你会得到 2 个进程.IIS 侦听所需的端口,而 Kestrel 侦听另一个端口.您的请求应该转到 IIS,然后转发到 Kestrel(使用 MS-ASPNETCORE-TOKEN
).
对 .UseIISIntegration()
的调用隐藏了这个映射.它实际上会更改应用程序中的端口,并在所需端口上设置 IIS
.但是如果你以错误的顺序调用这两种方法,它就会中断.
您收到此错误消息是因为 Kestrel 希望在 IIS
后面运行,并收到了直接请求.它注意到,因为 IIS
没有注入 MS-ASPNETCORE-TOKEN
标头.
此问题记录了该问题,可能会在未来版本中解决.
I added the line .UseUrls("http://*:5000")
to enable clients from other hosts accessing the web api.
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseUrls("http://*:5000") // Added
.Build();
host.Run();
}
However, using browser to access localhost:5000/api/Test
got the error of HTTP/1.1 400 Bad Request
? Should the .UseUrls()
be only compiled for production?
HTTP/1.1 400 Bad Request Date: Mon, 08 Aug 2016 21:42:30 GMT Content-Length: 0 Server: Kestrel
The following messages are copied from Visual studio Output window when testing.
You should call first .UseUrls()
and/or .UseConfig()
and then .UseIISIntegration()
.
When running ok under IIS/IISExpress, you end up with 2 processes. IIS listening on the desired port and Kestrel on another one. Your requests should go to IIS and then it is forwarded to Kestrel (with the MS-ASPNETCORE-TOKEN
).
The call to .UseIISIntegration()
hides this mapping. It actually changes the port in your app and sets IIS
on the desired port. But it breaks if you call both methods in incorrect order.
You are getting this error message because Kestrel expected to run behind IIS
, and received a direct request. And it noticed that because IIS
was not there to inject the MS-ASPNETCORE-TOKEN
header.
This issue documents the issue and may solve it in future releases.
这篇关于更改端口号后无法在 Visual Studio 中进行调试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!