在prod和dev中指定asp.net core 1.0 WebAPI.exe应该在program.cs中使用的url(端口) [英] Specifying the url (port) that a asp.net core 1.0 WebAPI.exe should use in program.cs for both prod and dev

查看:144
本文介绍了在prod和dev中指定asp.net core 1.0 WebAPI.exe应该在program.cs中使用的url(端口)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在asp.net core 1.0 Web api(.NET Framework)program.cs中执行以下操作,以指定要让我的Web api exe运行于哪个端口仅用于开发目的:

I am doing the following in my asp.net core 1.0 web api (.NET Framework) program.cs to specify which port I want my web api exe to run in for development purposes only:

public static void Main(string[] args)
{
    var host = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .UseUrls(new string[1] { "http://*:12012" })
        .Build();

    host.Run();
}

但是,当我发布到生产环境时,此行导致WebAPI错误,因为我希望exe使用生产网络API网址,即productionWeb/api/values而不是localhost:12012/values

However, when I publish to production this line causes the WebAPI to error since I want the exe to use the production web-api url i.e. productionWeb/api/values rather than localhost:12012/values

无论如何,我是否能够充分利用这两个世界,就可以指定我希望它在端口12012上运行以进行开发,而在生产url中进行生产?

Is there anyway I can get the best of both worlds being able to specify that I want it to run on port 12012 for development purposes and the production url for prod purposes?

我当前的解决方案是在发布前先注释掉该行.

My current solution is to just comment out the line before publishing.

推荐答案

使用IIS时,您正在覆盖url(IIS(AspNet核心模块)告诉应用程序通过在.UseIISIntegration()之后调用.UseUrls()进行监听).您应该更改这两个调用的顺序,以使.UseIISIntegration().UseUrl()之后.如果您未在IIS上运行,.UseIISIntegration()不会触摸您设置的url,因此在开发中,您的应用程序仍将在端口12012上侦听.在使用IIS运行时,.UseIISIntegration()将覆盖该url以在IIS告诉其侦听的端口上侦听在.我写了有关运行Asp的帖子带有IIS和Azure网站的.NET Core应用,说明了工作原理,包括这种细微差别.

When using IIS you are overwriting the url the IIS (AspNet Core Module) told the app to listen to by calling .UseUrls() after .UseIISIntegration(). You should change the order of these two calls so that .UseIISIntegration() is after .UseUrl(). .UseIISIntegration() will not touch urls you set if you are not running with IIS so in development your application still will be listening on port 12012. When running with IIS .UseIISIntegration() will overwrite the url to listen on the port IIS told it to listen on. I wrote a post on running Asp.NET Core apps with IIS and Azure Websites which explains how things work including this nuance.

这篇关于在prod和dev中指定asp.net core 1.0 WebAPI.exe应该在program.cs中使用的url(端口)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆