VSCode webapi default implementation does not run Swagger interface by default, 404 error occurs - api

When installing and running a new webapi template in Visual Studio Code:
dotnet new webapi -n weatherapi
the framework is set up to default to launch the Swagger interface, but subsequently executing dotnet run or launching with F5 will attempt to browse to the applicationUrl property specified in launchSettings.json. This subsequently throws a 404 error since this is not the Swagger definition url.
Also within launchSettings.json, launchBrowser defaults to true, and launchUrl defaults to swagger.
According to the documentation, the first entry in the profiles section of launchSettings.json will be the profile used unless specified otherwise in launch.json (which it is not).
So the question is, why does the default implementation throw a 404 when it looks like it should be set up by default to land on the Swagger interface?
The out-of-the-box launchSettings.json implementation is:
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:21895",
"sslPort": 44320
}
},
"profiles": {
"weatherapi": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7205;http://localhost:5111",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

Related

AspCore 404 on production enviroment

When I create AspCore Hosted Blazor Wasm template project(VS22) and change server's launchSettings.json aspnetcore enviroment to "Production" or delete it entirely, page gets 404. It runs fine on "Development". Am i missing something? Do i need to configure anything else to run the app on production enviroment?
In my case helped adding StaticWebAssetsLoader.UseStaticWebAssets() to Program.cs in BlazorApp.Server.proj:
using Microsoft.AspNetCore.Hosting.StaticWebAssets;
...
var builder = WebApplication.CreateBuilder(args);
StaticWebAssetsLoader.UseStaticWebAssets(builder.Environment, builder.Configuration);
...
We don't know the content in your launchSettings.json. And I follow your steps to try, and can't reproduce the issue.
So my suggestion is you need upgrade your VS2022 to latest version first.
Then you can create another new sample project to test.
If you not use the latest .net core version, please use .net6. (Same as mine)
My Test Steps:
Delete the environment like below:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:14136",
"sslPort": 44320
}
},
"profiles": {
"BlazorApp1": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "https://localhost:7060;http://localhost:5060",
"environmentVariables": {
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"environmentVariables": {
}
}
}
}
Run it in VS2022, and it works well.
Publish it, and host in IIS.

How to add HTTP endpoint to ASP.NET Core 6 app?

I created an ASP.NET Core 6 app with "Configure for HTTPS" checked during project creation. For testing purposes I now need a plain HTTP endpoint. How can I add such an endpoint?
My code currently looks like this:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello World! - use /graphql");
app.Run();
Serhii and Serge pointed me to the launchSettings.json (note that it's inside the Properties folder!). That's how it looks like:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:20380",
"sslPort": 44329
}
},
"profiles": {
"TestProject": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7090;http://localhost:5090",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Note the line
"applicationUrl": "https://localhost:7090;http://localhost:5090",
and note that the second URL is plain HTTP. I could call this URL without any issues (started project normally using F5, HTTPS link is called automatically, but one can just switch manually to the HTTP link in the browser).
So, nothing to do for me, no configuration change was needed etc :-)

Possibility to debug both HTTP and HTTPS in Kestrel and .NET Core like in .Net 4.x under IIS

I need to debug external authentication and it requires HTTPS. At the same time for most internal requests http is enough. There was no problem to listen on 80 and 443 ports when hosting my web app on IIS, but with ASP.NET Core hosted by Kesterl as I see it, the port is strictly bind to specific configuration in launchSettings.json such as:
"localCalls": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:40000"
},
"externalIdentity": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:50000"
}
I'm curiuse, is it possible to have listeners on both ports at a time without necessarity to relaunch in the other configuration to change the protocol.
According to the documentation you can define the endpoints within the same configuration:
"localCalls": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_URLS": "http://localhost:5000;https://localhost:5100"
}
}
Asp.Net Core 2.1 and onwards makes it super easy to use local SSL. When you create the project using Visual Studio, it asks you if you need to enable the SSL or not. If you selected that option before creating the project, you should see your launchSettings.json file something like below:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:61110",
"sslPort": 44377
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"YourProjectName": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}
The iisSettings section is for IISExpress with an sslPort defined i.e. 44377 in this case. So when your project runs under IISExpress, it uses that settings
The YourProjectName section is for Kestrel. You can see that applicationUrl is using both http and https endpoints. So when you do dotnet run you should see
Hosting environment: Development
Content root path: C:\users\...\YourProjectName
Now listening on: https://localhost:5001
Now listening on: http://localhost:5000
In addition, under Configure method, you should see a line below, so it will automatically redirect HTTP to HTTPS
app.UseHttpsRedirection();
If your launchSettings.json file doesn't look like the above. Try changing it in the project properties and enable the SSL, like in the screenshot below. When you save the settings, the launchSettings.json file will be updated
If the above doesn't work, try changing the launchSettings.json file manually. I hope that helps

After set ASPNETCORE_ENVIRONMENT Production in Startup file IHostingEnvironment environment not getting updated

I am using ASP.NET Core WebSite which targets the .NET Framework 4.6.1 and what all way i tried to update Environment.
1.Updated in Project properties Debug ASPNETCORE_ENVIRONMENT value as Production
2.Manually setting in launchSettings.json
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iis": {
"applicationUrl": "http://localhost/MyApp",
"sslPort": 0
},
"iisExpress": {
"applicationUrl": "http://localhost:53996/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "Executable",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
},
"MyApp": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
},
"applicationUrl": "http://localhost:53997/"
}
}
}
3.Windows Environment Variable
4.Setting through command prompt where csproj file is present.
using the command set ASPNETCORE_ENVIRONMENT=PRODUCTION
but after setting Production in above configuration setps still Enviroment is coming Development in Startup.cs file IHostingEnvironment.
I don't know i'm new Any other configuration i need to configure.
Any help.
Edit:
1.After Setting Windows Environment Variable under system level.
2.And the After while Restarting Computer System,Project is taking Environment Setting value whether it Production or Development.

Run ASP.NET Core application in "Development" environment

I am using ASP.NET Core RC2 and when I run dotnet run my application always runs in "Production". I am not able to change it to "Development".
I have the following launchSettings.json file:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:26088/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"MVCCoreRc2App": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
I am not sure why dotnet is running the application in "Production" when I am setting "ASPNETCORE_ENVIRONMENT": "Development".
This was working in ASP.NET Core RC1. What am I missing?
launchsettings.json is used when launching from Visual Studio, but not from the command line dotnet.exe.
On the console set the environment variable before calling dotnet run.
set ASPNETCORE_ENVIRONMENT=Development
If you're using Bash, the appropriate line is:
export ASPNETCORE_ENVIRONMENT=Development
You can set this in your ~/.bashrc file to make it apply whenever you log in.
You can even change the environment at the command line when you run your application as in:
dotnet run environment=development