ASP.NET Core using multiple urls in single application - asp.net-core

I'm creating new ASP.NET Core application and it run on https://localhost:44382/. I want to set multiple urls to browse my application like site1.testing.com, site2.testing.com, site3.tseting.com.
Everytime I browser those urls , I want to redirect to my application.
I found this setting in launchSetting.json
"myCoreApp": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
as you see
"applicationUrl": "https://localhost:5001;http://localhost:5000",
By referencing this , I've tried changing in this setting and doesn't work.

You cannot declare more than 1 URL for HTTP, and 1 URL for HTTPS protocol. (exclude case: you use different environment parameter).
Recommend for you (also is best practice):
Use NGINX server block, or
Apache HTTP Server virtual host.
Reference:
https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/
https://httpd.apache.org/docs/2.4/vhosts/examples.html
https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/launchsettings.json#L99

Related

How to test UseHttpsRedirection setting locally in .NET Core

In my ASP.NET Core project, I have turned ON HTTPS Redirection, with this setting in my Program.cs:
app.UseHttpsRedirection();
I have referred to MS doc.
Locally, when I run my Core Web project, it uses https by default (https://localhost:7432/). Now, to test if redirection from http -> https works, I browse to http://localhost:7432/, but I get a "This page isn’t working right now" error.
So, how do I test if this redirection is working locally?
In the Properties/launchSettings.json file, you'll see a property for applicationUrl that looks like this:
"applicationUrl": "https://localhost:7250;http://localhost:5097"
This sets up the app to listen on two different ports: 7250 for HTTPS and 5097 for HTTP. In your specific project, find the HTTP URL and use that to test the HTTPS redirection locally.
As far I know, you can't share the same port for both http and https. Instead, you should define the ports in the appsettings.json or appsettings.Production.json. FYI, I never define such thing in appsettings.json, because that file is typically part of the repository. To me, the best place is in the other file.
In one of those files, you should see (or add whereas not present) something like this:
"Kestrel": {
"EndPoints": {
"Http": {
"Url": "http://*:5000"
},
"Https": {
"Url": "https://*:5001",
"Certificate": {
"Path": "(your certificate file)",
"Password": "(your certificate password)"
},
}
}
},
"AllowedHosts": "*",
"AllowInvalid": true,
"https_port": 443,
...
Doing so, when you try to call your site as http, it will attempt to switch to the other port, using https.
For sure not the best explaination, but that's what I know about the https redirection.

Is there a way to set the launchUrl programmatically in Program.cs/Startup.cs?

Is there a way to set the initial url for ASP.NET Core programmatically? Preferably in the Startup.cs/Program.cs file?
Essentially, I'm after the same feature as the launchUrl property in the launchsettings.json file:
{
"profiles": {
"Console": {
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"launchUrl": "/fred",
},
}
}
What I want to do is add some code that will only run in DEBUG mode and alter the launchUrl based on a setting I have in my user secrets (avoiding any chance of accidentally adding this ad hoc config to the repo).
Does anyone know if this is possible?
If you are developing in visual studio then it have defined preprocessor in build configuration So you can just embed the lines in program.cs for .Net 6
#if DEBUG
builder.WebHost.UseUrls("http://localhost:5050", "https://localhost:5051"); // just put your urls from secret here
#endif
For other versions just find out where webhost is built. There will be some familiar codes like code below or so on.
builder.Services.AddControllersWithViews();

How to setup a test domain which points to my Controller runs locally

I have a C# controller which is running at port 44347. And when I go to browser locally https://127.0.0.0.1:44347/myurl, it hits my Controller runs on the same machine.
I want to setup so that I can when I load https://mytest.mycom.com:44347/myurl locally, it hits my controller run locally.
I have added 'mytest.mycom.com 127.0.0.1' to my hosts file in Windows. And I verify that ping mytest.mycom.com , it has reply.
But when I go https://mytest.mycom.com:44347/myurl locally, I get message saying 'mytest.mycom.com' took too long to respond.
Can you please tell me what am I missing?
You can change the url that your application uses within launchSettings.config (under the properties folder). Following is an example -
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:39655/",
"sslPort": 44340
}
},
Changing "applicationUrl" will allow you to use your test domain locally.

ASP.NET Core 2.2 ERR_CONNECTION_REFUSED for http

I am struggling to get an ASP.NET Core 2.2 application to listen for http requests.
When I start the application dotnet run --urls http://0.0.0.0:5000
the command returns Now listening on: https://[::]:5001
And when trying to access the url: http://localhost:5000
I get an error message in the browser stating ERR_CONNECTION_REFUSED.
Https connections do work on port 5001, but Http on port 5000 fail.
My launcSettings.json looks like this:
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "http://localhost:5000;https://localhost:5001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Please help by suggesting how to enable http.
I have disabled the HttpsRedirection in the Starup.cs, but this also has no effect:
app.UseStatusCodePages();
//app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
To fix this scenario, ensure that the Program class configures the WebHost correctly.
In the example below, two URL strings .UseUrls("https://*:5001;http://*:5000")
are provided to configure the WebHost, ensuring the service accepts both https and http on the tcp ports 5001 and 5000.
e.g.
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseUrls("https://*:5001;http://*:5000")
.UseStartup<Startup>();
}

how to access endpoints from a remote machine

I am running an ASP.NET Core 2.2 API on a Ubuntu 18.04x64 server that has a public IP address. It was developed on a Windows machine, and while running on IIS, all the endpoints work fine with the localhost. Once it is on the ubuntu server running using the dotnet command, I can't access the endpoints from my windows machine via the public IP address.
I tried adding this to my Program.cs file:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseUrls("http://*:5000")
.Build();
As well as
"WebAPI": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/values",
"applicationUrl":
"https://localhost:5001;http://localhost:5000;http://*:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
to my launchSettings.json
When I go to the public IP address in my browser, I get the welcome to nginx webpage, but when I go to /api/ping I get a 404 error. What else am I missing?