How to add HTTP endpoint to ASP.NET Core 6 app? - asp.net-core

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 :-)

Related

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

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"
}
}
}
}

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.

SwaggerUI not building - blank page in browser ASP.NET Core API

I'm have api app using ASP.NET Core. I'm also using swaggerUI and problem is when I start debugging application, browser open and just blank page appears. It should open swaggerUI but nothing happen. Using IIS Express. There's also no exception trown, any other error etc.
What can be a problem?
This is my launchSettings.json:
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:3000",
"sslPort": 44321
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
my project debug options
EDIT:
this is how it looks in browser
SwaggerUI will not register as default start page. You need to browse /swagger manually.
If you want SwaggerUI to open automatically upon starting a debug session, go to you project settings -> debug -> "absolute or relative URL" textbox. Insert swagger there.

Asp.NETCore 3.1 With Swagger launchUrl does not work after Publish

ASP.NET Core Web API project is configured with Swagger. When I run the project on my local machine the launchUrl works correctly and automatically redirects to my Swagger apidocs location (https://localhost:44373/apidocs/index.html)
But as soon as I publish the project on Azure the launchUrl no longer works correctly.
(https://myapiurl.com) => Should'nd this redirect automatically to (https://myapiurl.com/apidocs/index.html) ?
What am I missing for the published environment?
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:60837",
"sslPort": 44373
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "apidocs",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
},
"Web.Apis.Organization": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "apidocs",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}
My COnfigure Method
app.UseStaticFiles();
// Enable middle-ware to serve generated Swagger as a JSON endpoint. https://github.com/domaindrivendev/Swashbuckle.AspNetCore#generate-multiple-swagger-documents
app.UseSwagger(options =>
{
options.RouteTemplate = "apidocs/{documentName}/apispec.json";
});
//Enable middle-ware to serve swagger - ui(HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
// https://dejanstojanovic.net/aspnet/2018/november/setting-up-swagger-to-support-versioned-api-endpoints-in-aspnet-core/
// https://github.com/microsoft/aspnet-api-versioning/issues/516
app.UseSwaggerUI(c =>
{
c.RoutePrefix = "apidocs";
//Build a swagger endpoint for each discovered API version
foreach (var apiDescription in apiVersionProvider.ApiVersionDescriptions)
{
c.SwaggerEndpoint($"/{c.RoutePrefix}/{apiDescription.GroupName}/apispec.json", $"Version { apiDescription.ApiVersion.ToString()}");
}
c.DisplayRequestDuration();
c.DocumentTitle = GlobalConstants.OrganizationFullName;
// Custom Index
c.IndexStream = () => GetType().Assembly.GetManifestResourceStream("Web.Apis.Organization.wwwroot.swagger_ui.index.html");
// Custom style
//c.InjectStylesheet("/swagger-ui/custom.css");
//c.InjectJavascript("/swagger-ui/custom.js");
});

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