Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Swagger UI based on a Web.Config setting. #1417

Open
vbbalaji opened this issue Aug 10, 2021 · 0 comments
Open

Enable Swagger UI based on a Web.Config setting. #1417

vbbalaji opened this issue Aug 10, 2021 · 0 comments

Comments

@vbbalaji
Copy link

vbbalaji commented Aug 10, 2021

VERSION:

5.6

My code is a .Net REST call sitting on IIS. So its not a service. Here is my SwaggerConfig.cs

`
[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]

namespace myRESTService
{
public class SwaggerConfig
{
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
//string env = ConfigurationManager.AppSettings["Environment"];
//if (env != "NSAV")
//{
// return;
//}
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "myRESTService");
c.IncludeXmlComments(GetXmlCommentsPath());
c.OperationFilter();
})
.EnableSwaggerUi(c =>
{
c.EnableApiKeySupport("Authorization", "header");
});
}

    private static string GetXmlCommentsPath()
    {
        //string env = ConfigurationManager.AppSettings["Environment"];
        //if (env != "NSAV")
        //{
        //    return "";
        //}
        return System.AppDomain.CurrentDomain.BaseDirectory + @"bin\myRESTservice.xml";
    }
}

`
The commented portion was my attempt to restrict Swagger if the env variable is not NSAV. This variable is set by Azure. So basically if its in production server, it will not start swagger at all. If its on our dev (NSAV) server, then client can use the swagger.

I did see an example here, but that was for .Net Core and not for my usecase. Any suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant