自托管 Web 服务无加载

Self-Hosted Web service no loading

我只是想打开 运行 我的团队项目之一,它是配置为自托管的 .NET Web API 项目。其配置如下所示:

var host = new WebHostBuilder()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseKestrel()
                .UseStartup<Startup>()
                .UseUrls("http://0.0.0.0:3434")
                .Build();

            host.Run();

[Fiddler] The connection to '0.0.0.0' failed. Error: AddressNotAvailable (0x2741). System.Net.Sockets.SocketException The requested address is not valid in its context 0.0.0.0:3434

我不知道还能做什么。有什么建议吗?

尝试使用

var host = new WebHostBuilder()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseKestrel()
            .UseStartup<Startup>()
            .UseUrls("http://*:3434")
            .Build();

host.Run();

源文档Introduction to hosting in ASP.NET Core

Server URLs string

Key: urls. Set to a semicolon (;) separated list of URL prefixes to which the server should respond. For example, http://localhost:123. The domain/host name can be replaced with "*" to indicate the server should listen to requests on any IP address or host using the specified port and protocol (for example, http://*:5000 or https://*:5001). The protocol (http:// or https://) must be included with each URL. The prefixes are interpreted by the configured server; supported formats will vary between servers.

new WebHostBuilder()
    .UseUrls("http://*:5000;http://localhost:5001;https://hostname:5002")

主机启动后 运行 现在需要确保控制器配置了正确的路由并调用了正确的 URL,否则将返回 404 Not Found

例如下面的控制器

[Route("")]
public class RootController : Controller {
    [HttpGet] //Matches GET /
    public IActionResult Get() {
        return Ok("hello world");
    }

    [HttpGet("echo/{value}] //Matches GET /echo/anything-you-put-here
    public IActionResult GetEcho(string value) {
        return Ok(value);
    }
}

以上主机配置应分别匹配以下网址

http://localhost:3434/

http://localhost:3434/echo/stack-overflow