Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to Inject Javascript in Swagger

I get a 404 for a JavaScript file that I am trying to inject in my swagger. Following is my swagger config

var thisAssembly = typeof(SwaggerConfig).Assembly;

GlobalConfiguration.Configuration 
    .EnableSwagger(c =>
        {
            c.SingleApiVersion("v1", "A title for your API");
        })
    .EnableSwaggerUi(c =>
        {
            c.InjectJavaScript(thisAssembly,"MyApi.Api.SwaggerExtensions.inject.js");   
        });

For inject.js build action is set to embedded resource and logical path is correct as my project name is MyApi.Api and the file is in a folder within the project named SwaggerExtensions

like image 539
coder32 Avatar asked Jan 05 '17 20:01

coder32


2 Answers

When using custom resources the resource name should contain the default namespace of your project as described here. In your case the configuration should be:

c.InjectJavaScript(thisAssembly, "AcctMgmt.SwaggerExtensions.inject.js")
like image 106
venerik Avatar answered Oct 13 '22 18:10

venerik


I spent a lot of time trying to figure out that a method with the same name has a different behavior. The config in Startup.Configure expects a relative path from wwwroot:

public void Configure(IApplicationBuilder app) {
    //
    app.UseSwagger();
    app.UseSwaggerUI(c => {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "Salon API v1");
        c.InjectJavascript("/SwaggerExtension.js");
    });
}

Get started with Swashbuckle and ASP.NET Core

like image 39
Ivan Avatar answered Oct 13 '22 18:10

Ivan