Publish .NET CORE 5.0 API to IIS - c#

I've been struggling for these 2 days.
Does anyone know what's the problem? I tried to publish my .net core API to IIS, but always failed when browse the web.
I did the step to publish same as like this webpage.
After I browse on the server, http://127.0.0.1:5100/swagger/index.html , the page said No webpage was found. I am sure there no system/apps using the 5100 port.

After I browse on the server, http://127.0.0.1:5100/swagger/index.html , the page said No webpage was found.
If you check the code of Configure method in Startup.cs, you can find the app will only serve the Swagger UI on Development environment.
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebAppi v1"));
}
If you'd like to serve the Swagger UI on Production environment after you publish it on IIS, you can modify the code as below, then republish it to IIS.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebAppi v1"));
//...
Besides, if your IIS site is just for testing purpose, to quickly resolve the issue and make your site show the Swagger UI as expected, you can try to add an environment variable as below for your site.

Related

Unable to find an OpenAPI description

I am working the Microsoft Learn tutorials to "Create a web API with ASP.Net Core".
I used .NET5
I have a problem when I run the command:
httprepl connect http://localhost:5000
I am getting a response, "Unable to find an OpenAPI description".
And the following command "ls" returns me from it not trooping endpoints.
c:/xxx/Source/Repos/ContosoPizza
$ httprepl http://localhost:5000
(Disconnected)> connect http://localhost:5000
Using a base address of http://localhost:5000/
Unable to find an OpenAPI description
For detailed tool info, see https://aka.ms/http-repl-doc
http://localhost:5000/> ls
No directory structure has been set, so there is nothing to list. Use the "connect" command to set a directory structure based on an OpenAPI description.
I tried all the solutions offered by #Gowiser in the question launched by #Nuse Why is HttpRepl unable to find an OpenAPI description? The command "ls" does not show available endpoints
But nothing worked.
Working through Create a web API with ASP.NET Core Controllers i also did run into the error
Unable to find an OpenAPI description
The tutorial uses dotnet 6, i was using dotnet core 3.1.
I did the changes from sasha answers here and had to add these packages
dotnet add package Microsoft.OpenApi --version 1.2.3
dotnet add package Swashbuckle.AspNetCore.Swagger --version 6.2.3
dotnet add package Swashbuckle.AspNetCore.SwaggerUI --version 6.2.3
dotnet add package Swashbuckle.AspNetCore.SwaggerGen --version 6.2.3
After all that i still got error messages. In step 4 of Exercise - Create a web API project the following is written
Connect to our web API by running the following command:
httprepl https://localhost:{PORT}
Alternatively, run the following command at any time while the HttpRepl is running:
(Disconnected)> connect https://localhost:{PORT}
I was not aware that running httprepl like this
httprepl https://localhost:{PORT}
still requires that your web app is running. So you have to open a second terminal (cmd.exe) to run your webapi project dotnet run. After that you can connect to the swagger docs using httprepl like this
connect http://localhost:5000 --verbose --openapi /swagger/v1/swagger.json
http://localhost:5000/> ls
. []
WeatherForecast [GET]
http://localhost:5000/> cd WeatherForecast
/WeatherForecast [GET]
http://localhost:5000/WeatherForecast> get
HTTP/1.1 200 OK
This did the trick for me.
Adding the swagger ui
Swashbuckle.AspNetCore - Getting Started contains
Optionally, insert the swagger-ui middleware if you want to expose
interactive documentation, specifying the Swagger JSON endpoint(s) to
power it from.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("v1/swagger.json", "My API V1");
});
If you change startup.cs to include this
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c =>
c.SwaggerEndpoint("/swagger/v1/swagger.json", "ContosoPizza v1"));
}
....
You get a nice gui under http://localhost:5000/swagger/ which looks like this
I am assuming you are following the steps described in the 'Create a web API project' exercise of the Create a web API with ASP.NET Core course.
Before going through step 5. make sure in your project there is:
a services.AddSwaggerGen() call in the Startup.ConfigureServices() method
an app.UseSwagger() call in the Startup.Configure() method
Here is a working version of Startup.cs:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
namespace ContosoPizza
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "ContosoPizza", Version = "v1" });
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ContosoPizza v1"));
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
Calling ls in the httprepl session shows expected results:
❯ httprepl http://localhost:5000
(Disconnected)> connect http://localhost:5000
Using a base address of http://localhost:5000/
Using OpenAPI description at http://localhost:5000/swagger/v1/swagger.json
For detailed tool info, see https://aka.ms/http-repl-doc
http://localhost:5000/> ls
. []
WeatherForecast [GET]
http://localhost:5000/>
Here you can find a full project for this exercise.
Indeed #surfmuggle to connect to the API via httprepl, the API had to be running in another terminal, so 2 terminals in total:
1 to run the API with dotnet run
1 other to call instructions like ls or other later
run this command first:
dotnet dev-certs https --trust

How to correctly connect swagger definition to a subfolder of the deployed app to the hosting

I have a .net core web api application with swagger.
I'm using .net 5
Infrastructure:
I'm using smarterasp hosting services for react app and webapi app.
React app is on the root of the created (folder) website.
I just moved my webapi from my root folder to the folder api.
Note: the webapi worked ok, when it was initially in the root folder.
Problem:
As I mentioned, when I moved my webapi to the api subfolder, I'm getting the error:
Attempts:
I tried to change address in my Startup.cs class:
./swagger/v1/swagger.json
api/swagger/v1/swagger.json
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IConfiguration configuration)
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("./swagger/v1/swagger.json", "NNN.WebApi v1");
c.RoutePrefix = string.Empty;
});
...
}
but, I'm getting or this failure message or 404 not found..
You can try this code:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IConfiguration configuration)
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("../swagger/v1/swagger.json", "NNN.WebApi v1");
});
...
}
The solution was to put following endpoint:
c.SwaggerEndpoint("/v1/swagger.json", "NNN.WebApi v1");

ASP.NET Core CORS configuration not working for Firefox

I have the following CORS configuration for my ASP.NET Core (dotnet SDK 3) application. What I observe is that this configuration works great for Google Chrome (version 76.0.3809.100 64-bit). The Access-Control-Allow-Origin is missing from the response headers in Mozilla Firefox Developer Edition (version 69.0b14 64-bit). As a matter of fact, there isn't any response. Is there any solution for this?
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddNewtonsoftJson();
services.AddCors();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseCors(builder =>
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
);
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
I found the problem and it was a really weird one. I was reading an older blog which talked about CORS and ASP.NET Core. Then I stumbled upon this comment which said that the solution worked for Kestrel, but not for IIS Express. So I changed my build configuration to not use IIS Express and it now works flawlessly for both browsers. If you want this to work for IIS Express, then you don't edit your Startup.cs, but you edit your application.config file which probably is located in your project root folder.

app.UseDeveloperExceptionPage() doesn't seem to be working on ASP .NET Core on Ubuntu

Very simple ASP .NET core application so far. I've turned on the developer exception page as follows
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseDeveloperExceptionPage();
app.UseMvc();
}
I've deployed it on windows as a portable app and i'm able to see the errors on the developer page (awesome page by the way) when a specific web API request fails with an exception.
I publish the same app to Ubuntu as a self-contained app and I don't get the developer error page to show up (though the seems to run fine - i've hard coded an error just to test the developer page itself).
By default on macOS and Linux ASP.NET Core's Hosting environment is Production.
On Ubuntu you can start the application and set the Hosting environment from command line using
> dotnet run --environment=Development
For macOS:
$ ASPNETCORE_ENVIRONMENT=Development dotnet run
If you are using Visual Studio Code, in launch.json there's the following setting for each configuration:
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
The Developer Exception Page is for use when the environment is Development.
See https://www.exceptionnotfound.net/working-with-environments-and-launch-settings-in-asp-net-core/
The act of publishing the app to Ubuntu is setting the environment to Production.
This is the behavior I found: (ASP.NET Core version 6, on Windows)
When environment is Development, exception pages are always generated, regardless of whether UseDeveloperExceptionPage was called.
When environment is anything other than Development, exceptions pages are generated only if UseDeveloperExceptionPage was called.
Startup.cs is mostly procedural. In your Startup.cs
app.UseDeveloperExceptionPage();
should come before
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapRazorPages();
});
If you add it after then it won't function.

Asp.Net Core Web Application Static Files Gives 404 on Linux

I have created a web application using .net core. I have successfully get it running on Windows and Mac. However I get 404 on all my static files on Linux. I am using Ubuntu 16.04 and my startup.cs is like this.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory,IApplicationLifetime appLifetime)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
app.UseDefaultFiles();
app.UseStaticFiles();
appLifetime.ApplicationStopped.Register(() => this.ApplicationContainer.Dispose());
}
Any idea how to get it running on Linux?
Turns out directories are case sensetive.
The app was not able to resolve content/bootstrap.css. However, Content/bootstrap.css was resolving. So I renamed the folders and files accordingly and everything works fine.

Categories

Resources