How can I launch Swagger on the server?

pfdk 20 Reputation points
2023-09-15T06:30:26.3666667+00:00

Hello,

I use Visual Studio Community 2022 (64 bit) Version 17.5.3.

I check the C# application by Visual Studio.

If I run debug(Press F5 key), Swagger launches(https://localhost:7173/swagger/index.html).

01

The application works on the server(Amazon EC2 instance).

I tried to access the URL 'https://(server domain)/swagger/index.html', but page is not found.

This application was created by the person who left office, and its specifications are unknown.

I heard that they could access the URL 'https://(server domain)/swagger/index.html' before.

It may be necessary to change the application setting, but no one know how to do it.

Could you tell me how can I launch Swagger on the server?

Thanks,

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,239 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,698 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,383 questions
{count} votes

Accepted answer
  1. Xinran Shen - MSFT 2,091 Reputation points
    2023-09-15T09:28:42.25+00:00

    Hi, @pfdk , In Asp.Net Core, Swagger is default used in development environment, Check the below code in program.cs.

    if (app.Environment.IsDevelopment())
    {
          app.UseSwagger();
          app.UseSwaggerUI();
    }
    

    If you want to use swagger not only in development environment, You just need to delete the If() condition and use swagger middleware directly.

    app.UseSwagger();     
    app.UseSwaggerUI();
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Xinran Shen

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Johan Smarius 390 Reputation points MVP
    2023-09-15T09:30:34.02+00:00

    Swagger is normally only available in development mode. Your server runs in release mode. If you want swagger to be available on your server, you need to change some code in the program.cs class and then build and redeploy the code to the server.

    User's image

    The code between the brackets should be moved outside the if {} block.

    You do need to consider if you really want swagger to be available on the server, because if you can reach it, so can hackers, and hackers love to have this kind of information.

    1 person found this answer helpful.

  2. pfdk 20 Reputation points
    2023-09-19T03:34:25.54+00:00

    I'm sorry for my late reply. Thank you for your reply.

    I have changed program.cs as you explained.

    It works as I expected.

    I'm afraid, I have one more question. How did you know the setting?