How to setup a test domain which points to my Controller runs locally - asp.net-core

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.

Related

Why ASP.Net Core 7.0 Web API showing as Connection refused?

I am new to asp.net core 7.0.
I am working on the Web Api. However I created one controller with [ApiController] decoration.
In the launchSettings.json file it shows port number as 14023 as shown below :-
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:14023",
"sslPort": 44381
}
Means that my Web API will be pointing to http://localhost:14023
However when I run the application and navigate to http://localhost:14023
it shows below error :
This site can’t be reached
localhost refused to connect.
ERR_CONNECTION_REFUSED
And when I hit the same API URL through Postman then below error coming :-
Error: connect ECONNREFUSED 127.0.0.1:14023
Initially I thought that it could be related to CORS so I added below code in Program.cs file :-
builder.Services.AddCors();
app.UseCors(options =>
options.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
);
Can anyone help me on this?
I am stuck. Even I googled but did not find any suitable answer.

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();

ASP.NET Core using multiple urls in single application

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

.Net Core web api needs to consume web service from configuration file

In my .Net Core application I am consuming Web Service using
Add -> Connected services -> WCF Service Preview(nuget package) and added the web service and used the service methods.
However, now clients moved the Web Service to internal Web Servers and I do not have access to the service from my development environment. So I am not able to access the service methods and build my solution and publish.
Is there any way I can pass the Service URL from Configuration file?
Example:
For Dev Environment - http://dev.svc
For Prod Environment - http://prod.svc
Yes, you can. I suggest you to read whole article about the configuration in ASP.NET Core, as there are lots of things you may find useful. In general, you can get the config files for each of environments with code like this:
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
// note that here we do override the values by specific file for an emvironment
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
// and this line will get the environment variables from server machine
.AddEnvironmentVariables();
Configuration = builder.Build();
}
}
JSON files could be something like this:
appsettings.json
{
"serviceUrl": "",
}
appsettings.Development.json
{
"serviceUrl": "http://dev.svc",
}
appsettings.Production.json
{
"serviceUrl": "http://prod.svc",
}
Also you may find useful the Working with multiple environments article.