Search code examples
c#asp.net-web-apidocumentationversioning

Error when using aspnet-api-versioning library with documentation configuration


I'm using the aspnet-api-versioning library in my web API project. I follow the instruction from https://github.com/Microsoft/aspnet-api-versioning/wiki/API-Documentation. I added the code as explained in my WebApiConfig.cs

So

namespace Nppg.WebApi
{

using Microsoft.Web.Http.Versioning;
using Nppg.WebApi.ActionFilters;
using System.Web.Http;

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Add specific Json converter/formetters
        var jsonFormatter = config.Formatters.JsonFormatter;
        jsonFormatter.SerializerSettings.Converters.Add(new LinkDtoConverter());

        // Web API configuration and services
        // Configure Web API to use only bearer token authentication.
        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
        config.Filters.Add(new HttpLoggingFilterAttribute());
        config.MessageHandlers.Add(new LogRequestAndResponseHandler());

        #region API versioning configuration
        // allow a client to call you without specifying an api version
        // since we haven't configured it otherwise, the assumed api version will be 1.0
        // https://github.com/Microsoft/aspnet-api-versioning/wiki/New-Services-Quick-Start
        // added to the web api configuration in the application setup
        config.AddApiVersioning(options =>
        {
            options.ApiVersionReader = new MediaTypeApiVersionReader();
            options.AssumeDefaultVersionWhenUnspecified = true;
            options.ApiVersionSelector = new CurrentImplementationApiVersionSelector(options);
        });

        //This code must be used to register "apiVersion" as a contraint
        //var constraintResolver = new DefaultInlineConstraintResolver()
        //{
        //    ConstraintMap = { ["apiVersion"] = typeof(ApiVersionRouteConstraint) }
        //};

        // format the version as "'v'major[.minor][-status]"
        var apiExplorer = config.AddVersionedApiExplorer(o => o.GroupNameFormat = "'v'VVV");

        config.EnableSwagger(
            "{apiVersion}/swagger",
            swagger =>
            {
                swagger.MultipleApiVersions(
                    (apiDescription, version) => apiDescription.GetGroupName() == version,
                    info =>
                    {
                        foreach (var group in apiExplorer.ApiDescriptions)
                        {
                            info.Version(group.Name, $"Sample API {group.ApiVersion}");
                        }
                    });
            })
         .EnableSwaggerUi(swagger => swagger.EnableDiscoveryUrlSelector());



        #endregion

        // Web API routes
        //config.MapHttpAttributeRoutes(constraintResolver); // With URL Path Versioning
        config.MapHttpAttributeRoutes(); // Whithout URL Path Versioning

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}
}

I get this error message:

Error CS1061 'HttpConfiguration' does not contain a definition for 'AddVersionedApiExplorer' and no extension method 'AddVersionedApiExplorer' accepting a first argument of type 'HttpConfiguration' could be found (are you missing a using directive or an assembly reference?)

and

Error CS1061 'HttpConfiguration' does not contain a definition for 'EnableSwagger' and no extension method 'EnableSwagger' accepting a first argument of type 'HttpConfiguration' could be found (are you missing a using directive or an assembly reference?)

Any idea why I get these messages ?


Solution

  • Try installing the nuget package for Microsoft.AspNet.WebApi.Versioning.ApiExplorer at Here