I'm at a point where I really need API documentation for my WebAPI 2 project, and I used the Swashbuckle 5 NuGet package. Out of the box, I can hit {myrooturl}/swagger and a UI pops up, but there are no controllers, methods, or anything in there. Just my title: [ base url: /EM.Services , api version: v1 ]
I took a look at the Swashbuckle docs, and since I'm using OWIN which is hosted by IIS, I modified the SwaggerConfig with:
c.RootUrl(req => req.RequestUri.GetLeftPart(UriPartial.Authority) + req.GetRequestContext().VirtualPathRoot.TrimEnd('/'));
as per this doc: https://github.com/domaindrivendev/Swashbuckle/blob/1326e753ce9b3a823b3c156b0b601134692ffc58/README.md#transitioning-to-swashbuckle-50
I also setup the build of the project to generate the XML docs and pointed my SwaggerConfig to it with:
private static string GetXmlCommentsPath()
{
// tried with an without the \bin
return String.Format(#"{0}\bin\EM.Services.XML", AppDomain.CurrentDomain.BaseDirectory);
}
I'm not sure if the XML docs working/not-working has anything to do with it though, as I get absolutely no controllers on the swagger-ui page.
For what it's worth, all of my controller inherit from a BaseController, which in turn inherits from ApiController.
Is there something screwy with my WebApiConfig?
public static void Register(HttpConfiguration config)
{
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
config.Filters.Add(new ValidateModelAttribute());
config.Filters.Add(new BaseAuthenticationAttribute());
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
jsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
}
My concrete controllers all look like this (I've tried subbing out BaseController for ApiController and there is no change):
[RoutePrefix("api/whatever")]
public class FooController : BaseController
and my Base controller doesn't do much (yet), just has an attribute:
[BuildClaims]
public abstract class BaseController : ApiController
The empty page persists across using IIS Express or full blown IIS.
Update:
Example of a contrived controller I made that is really basic. It also does not show up, as I still have the boiler plate swagger ui with nothing in it.
/// <summary>
/// I am a test
/// </summary>
[RoutePrefix("api/dummy")]
public class DummyController : ApiController
{
[HttpGet]
[Route("foo")]
public int Foo()
{
return 42;
}
}
I got stuck.. and these answers didn't help me fully... although they led me there. Just to save other people some time:
You have to pass the http config from OWIN and then register on that instead of using the GlobalConfiguration class like so:
//starup.cs
public void Configuration(IAppBuilder app)
{
Config = new HttpConfiguration();
WebApiConfig.Register(Config);
app
.UseResponseLogging()
.UseRequestLogging()
.UseHttpErrors()
.UseExceptionLogging()
.UseWebApi(Config);
HandlerConfig.Register(Config);
SwaggerConfig.Register(Config);
}
and in the swagger config file, change the register method to:
public static void Register(HttpConfiguration config)
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
config
.EnableSwagger(c =>
{...
Hope this helps.
I found the problem. After creating an empty test project, I noticed that the WebApiConfiguration was being registered from the global.asax app start and not the OWIN startup class (like I did).
Since Swagger/Swashbuckle is hooking into the GlobalConfiguration and also given that OWIN startup and Global.asax live in different contexts (I think), the fix is to wire up your WebAPI stuff to register from Global.asax and to have OWIN's app object use WebAPI.
Relevant bits:
// global asax
protected void Application_Start(object sender, EventArgs e)
{
GlobalConfiguration.Configure(WebApiConfig.Register);
// ... more stuff
}
//startup.cs
public void Configuration(IAppBuilder app)
{
// This must happen FIRST otherwise CORS will not work.
app.UseCors(CorsOptions.AllowAll);
HttpConfiguration config = new HttpConfiguration();
ConfigureAuth(app);
// webapi is registered in the global.asax
app.UseWebApi(config);
}
After rewiring as above, I can now see controllers & actions in swagger UI.
I found that I had the same issue. I created an extension method to help
using Swashbuckle.Application;
using System.Web.Http;
public static class SwaggerExtensions
{
public static HttpConfiguration EnableSwagger(this HttpConfiguration httpConfiguration)
{
httpConfiguration
.EnableSwagger(c => c.SingleApiVersion("v1", "A title for your API"))
.EnableSwaggerUi();
return httpConfiguration;
}
}
Then in my Startup.cs
public class Startup
{
public void Configuration(IAppBuilder appBuilder)
{
HttpConfiguration httpConfiguration = new HttpConfiguration();
httpConfiguration
.EnableSwagger() // <==== EXTENSION METHOD <==== //
.MapHttpAttributeRoutes();
httpConfiguration.Routes.MapHttpRoute(
"DefaultApi",
"api/{controller}/{id}",
new {id = RouteParameter.Optional});
appBuilder
.UseWebApi(httpConfiguration);
}
}
I just had the same issue myself and none of these helped me.
After some messing around I figured out that the routes that I'd labeled as [System.Web.Mvc.Route("visit")] were not being discovered by swagger.
[HttpGet]
// ROUTE ATTRIBUTE NOT FOUND BY SWAGGER
[System.Web.Mvc.Route("visit")]
public string Visit()
{
but [System.Web.Http.Route("visit")] is
[HttpGet]
// ROUTE ATTRIBUTE *IS* FOUND BY SWAGGER
[System.Web.Http.Route("visit")]
public string Visit()
{
I'm not 100% sure, but if it matters, I also switched from
public class MyAPIController : Controller
to:
public class MyAPIController : System.Web.Http.ApiController
More accurately I removed the "using" statement for System.Web.Mvc, but the code is listed for illustrative purposes.
Hope this helps someone else in future :) Good luck!
All these solutions works for me, but all of them are just nasty hacks for my issue. After few hours of investigation I found out, that the problem is I also use Glimpse (or other packages which change route table).
Here is a great summary: https://github.com/domaindrivendev/Swashbuckle/issues/468#issuecomment-139246748
Glimpse adds castle proxies on top of HttpWebRoute. So HostedHttpRouteCollection is collection of RouteProxy and not
HttpWebRoute.
APIExplorer class has FlattenRoutes method which does a foreach loop over HostedHttpRouteCollection.
GetEnumerator implementation of HostedHttpRouteCollection specifically look for HttpWebRoute. See the code below. Since glimpse
has added proxies, enumerator always returns 0 routes!!
public override IEnumerator GetEnumerator()
{
// Here we only care about Web API routes.
return _routeCollection
.OfType()
.Select(httpWebRoute => httpWebRoute.HttpRoute)
.GetEnumerator();
}
I am affraid there is no solution, you can choose what you want to use: Swashbuckle or Glimpse, but not both together.
Of course you can try to run on one of these workarounds, but there is a risk of unexpected behaviour and tricky bugs.
Swashbuckle sits on top of WebApi's metadata layer ApiExplorer. It takes the operation descriptions from ApiExplorer and then maps them to Swagger descriptions.
Since your controller inherits from BASECONTROLLER and not APICONTROLLER it will not work
Per JimWolleys comment
private IEnumerable<ApiDescription> GetApiDescriptionsFor(string apiVersion)
{
return (_options.VersionSupportResolver == null)
? _apiExplorer.ApiDescriptions
: _apiExplorer.ApiDescriptions.Where(apiDesc => _options.VersionSupportResolver(apiDesc, apiVersion));
}
this is the method that powers Swashbuckle to get all of the api calls. It takes an IApiExplorer. Which if it wasnt modified to take something different it takes the default ApiExplorer provided. Which only has information about things which inherit from ApiController
Swashbuckle git repo. just search for GetApiDescriptionsFor and it will take you straight to the method
I had tons of issues with Owin + Swashbuckle integration and none of these answers fixed everything for me. Long story short, I managed to solve everything and created an open source repo to be used as a template for anyone who needs it.
Please check: ASPSwaggerOwinTemplate
I had this issue as well using OWIN. The issue was resolved by installing only Swashbuckler Core as suggested in here and by editing the Startup.cs as below:
// Startup.cs
// ...
HttpConfiguration config = new HttpConfiguration();
// ...
config
.EnableSwagger(c =>
{
////add if there's custom root path
//c.RootUrl(req =>
// req.RequestUri.GetLeftPart(UriPartial.Authority) +
// req.GetRequestContext().VirtualPathRoot.TrimEnd('/'));
c.SingleApiVersion("v1", "A title for your API");
})
.EnableSwaggerUi();
// ...
appBuilder.UseWebApi(config);
I found this link to be very helpful. This particular solution is specific to a Microsoft.Azure.Mobile.Server API but it solved the problem for me.
Azure Mobile Apps Server and Swagger
I was familiar with the .NET core version of Swashbuckle which auto-expanded the controllers. When I was working on a framework (non-core) API, when I finally managed to get something showing, I was confused because I didn't know to click show/hide and still thought it wasn't working.
You can have it expanded by default with the following:
.EnableSwaggerUi(c => {
c.DocExpansion(DocExpansion.List);
});
In my case I had a similar issue to Alex C. I had to do 2 things to fix it:
The 1st things was I had an import statement about using MVC, something like this:
using System.Web.Mvc;
I removed that import statement and that solved half the problem. The other thing I noticed is that in one of the controllers that was showing up in Swashbucke had an annotation like this
[RoutePrefix("v1/Awesome")]
Where Awesome is the name of the controller AwesomeController. So I put that route prefix annotation right before my class declaration and now it shows up in the Swagger interface
[RoutePrefix("v1/Amazing")]
public class AmazingController : ApiController
So if anyone else is having this issue you might check if you need to add a route prefix like I did.
Related
Why does adding versioning to a webApi project removes number from the controller path name?
Replication steps :
Create a fresh .net6 project. And rename WeatherForecastController to WeatherForecast2Controller.
Run app and call https://localhost:x/WeatherForecast2 (where x is your port)
Observe valid/expected results
Add the below code in program.cs/startup
services.AddApiVersioning(config =>
{
config.AssumeDefaultVersionWhenUnspecified = true;
config.DefaultApiVersion = new ApiVersion(1, 0);
config.ReportApiVersions = true;
config.ApiVersionReader = ApiVersionReader.Combine(
new QueryStringApiVersionReader("version"),
new HeaderApiVersionReader("x-version"));
config.UseApiBehavior = false;
});
Run app and call https://localhost:x/WeatherForecast2 (where x is your port).
Step 5 will return a 404 not found error.
However if you call https://localhost:x/WeatherForecast. It will work.
So why does adding versioning, change the url path?
It's not entirely clear if you are using <= 5.0 or 6.0+.
History
The reason this behavior happens is because the only logical way to group controllers together is by their names. A controller name, therefore, becomes very important. This is problematic in code because two or more controllers in the same namespace cannot have the same type name. The assumption and long-defined convention has been to allow ASP.NET to remove the Controller suffix and then remove any remaining numbers. This allows ValuesController, Values2Controller and Values3Controller to all map to the logical controller name Values by default. In most cases, that's probably want someone wants. If API Versioning doesn't do this, then there is no way to collate all API versions together for an API.
Contrary to popular belief, route templates are not considered for grouping controllers (e.g. APIs). There is too much ambiguity as to how a template can map to code. Take the simplest example of two different versions of the same API with different route templates: V1 = values/{id}, V2 = values/{id:int}. These are semantically equivalent, but not the same. API Versioning does not try to understand what the route template means nor compare their equivalence. It can easily get a lot more complicated; especially, for overlapping route templates. For example, should order/{oid}/customer/{cid} be part of the Orders API or the Customer API? Only the service author knows for sure.
Regression
In the 5.0 release, a regression was accidentally introduced due to an over-optimization. The controller name is used in two places: the actual name of the controller and the name used to group controllers. It seems reasonable they'd be the same and why normalize (e.g. trim suffixes) more than necessary? It seemed like a good idea, but it caused unexpected behavior - such as this one. There are also legitimate reasons to have a number in the name of a controller; for example, S3Controller.
Fix
In library versions <= 5.0, developers had no control over the behavior of how names were normalized. In 5.1 and 6.0+, this is now exposed via the IControllerNameConvention service, which has two methods: one for normalizing the controller name and one for normalizing the group name. The following implementations are provided out of the box as properties on ControllerNameConvention:
Default: The default, out-of-the-box conventions
Original: The original names without any normalization (could result in the wrong behavior)
Grouped: The group name is normalized, but the controller name is unmodified
If none of those work for you, then you can create your own custom convention. In 5.1 this is wired up via ApiVersioningOptions.ControllerNameConvention, while in 6.0+ IControllerNameConvention is a transient service in the DI container.
Workaround
There are two ways you can workaround the problem using the current version you are leveraging:
Explicit Route Template
If you omit using the [controller] token, the routing problem will be resolved; for example, api/weatherforecast. You appear to have already discovered this.
Explicit Controller Name
The controller name is derived from a convention, even without API Versioning. It was understood this behavior could be a problem so API Versioning provides a way to explicit set it with the ControllerNameAttribute.
[ControllerName("WeatherForecast")]
[Route("api/[controller]")] // ← expands to 'api/WeatherForecast'
public class WeatherForecast2Controller : ControllerBase { }
Edge Case
This will solve the routing issues, but it will not fix the controller name issue. That should only matter if you are planning on documenting your API with OpenAPI (formerly Swagger). For example, S3Controller will simply show up as S, even though the route might be api/s3.
I think that your controller may have issues, as I used your AddApiVersioning code and it works for me. To avoid the conflicting action names, you can two different controllers.
ApiController
namespace WebApiVersioningApp.Controllers;
[ApiVersion("1.0")]
[ApiVersion("2.0")]
[ApiController]
[Route("api/Version2")]
public class Version2Controller : ControllerBase
{
[MapToApiVersion("1.0")]
[HttpGet(Name = "GetWeatherForecastV1")]
public string GetV1()
{
return "Version 1";
}
[MapToApiVersion("2.0")]
[HttpGet(Name = "GetWeatherForecastV2")]
public string GetV2()
{
return "Version 2";
}
}
Program:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First())
);
// Versioning setup
builder.Services.AddApiVersioning(o =>
{
o.ReportApiVersions = true;
o.AssumeDefaultVersionWhenUnspecified = true;
o.DefaultApiVersion = new ApiVersion(1, 0);
o.ApiVersionReader = ApiVersionReader.Combine(
new QueryStringApiVersionReader("version"),
new HeaderApiVersionReader("x-version"));
o.UseApiBehavior = false;
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Hope this works for you.
I'd like to prepare my .NET Core Web API project so that multiple versions of the API can be managed and documented, according to the REST services standards.
I'm using .NET Core 2.1 with NSwag (v11.18.2). I also installed the Microsoft.AspNetCore.Mvc.Versioning NuGet package.
I already searched with Google for some configuration examples, but the only useful link I found is this.
I'm now able to get Swagger pages for both API versions but with some problems:
Please note that none of the last config settings (Title, Description, etc.) takes effect on any of the 2 routes. It only works if I add them on each of the individual configuration. So I'd also like to know if it possible to avoid that, since the general configuration of the API can be version indipendent (title, description and so on...).
Since the issue with NSwag and Microsoft API Versioning package discussed in the above link, was opened 2-3 months (and NSwag versions too) ago, I'd like to know if it is now truly fixed and in this case, which is the right configuration to set.
Although the version is explicit in the configuration of the controllers, it is still required as a mandatory input parameter of the controller methods and of course I don't want that! See image:
So, my actual configuration, by following that example, is looking like this:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddApiVersioning(options =>
{
options.AssumeDefaultVersionWhenUnspecified = true;
options.DefaultApiVersion = new ApiVersion(1, 0);
options.ReportApiVersions = true;
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSwaggerWithApiExplorer(config =>
{
config.GeneratorSettings.OperationProcessors.TryGet<ApiVersionProcessor>().IncludedVersions = new[] { "1.0" };
config.SwaggerRoute = "v1.0.json";
});
app.UseSwaggerWithApiExplorer(config =>
{
config.GeneratorSettings.OperationProcessors.TryGet<ApiVersionProcessor>().IncludedVersions = new[] { "2.0" };
config.SwaggerRoute = "v2.0.json";
});
app.UseSwaggerUi3(typeof(Startup).GetTypeInfo().Assembly, config =>
{
config.SwaggerRoutes.Add(new SwaggerUi3Route("v1.0", "/v1.0.json"));
config.SwaggerRoutes.Add(new SwaggerUi3Route("v2.0", "/v2.0.json"));
config.GeneratorSettings.Title = "My API";
config.GeneratorSettings.Description = "API functionalities.";
config.GeneratorSettings.DefaultUrlTemplate = "{v:apiVersion}/{controller}/{action}/{id?}";
config.GeneratorSettings.DefaultPropertyNameHandling = PropertyNameHandling.CamelCase
});
}
And these are my actual controllers:
[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]/[action]")]
[SwaggerTag("Test1", Description = "Core operations on machines (v1.0).")]
public class MachinesController : Controller
{
[HttpGet("{id}")]
[ProducesResponseType((int)HttpStatusCode.OK)]
public async Task<ActionResult<Machine>> Get(int id)
{
return await ...
}
}
[ApiController]
[ApiVersion("2.0")]
[Route("api/v{version:apiVersion}/[controller]/[action]")]
[SwaggerTag("Test2", Description = "Core operations on machines (v2.0).")]
public class MachinesController : Controller
{
[HttpGet("{id}")]
[ProducesResponseType((int)HttpStatusCode.OK)]
public async Task<ActionResult<Machine>> Get(int id)
{
return await ...
}
}
They are ignored in the middleware because they are inferred from the settings or do not apply for api explorer (template). However title and description should work...
Please create an issue with the specific issue and a repro, also check out the existing tests in the repo
Fixed with v11.18.3
I believe starting in NSwag 12.0.0, there is significantly improved support for the API Explorer. It's important that the complementary API Explorer package for API versioning is also referenced so that the proper information is provided to NSwag.
The Swagger sample application provided by API Versioning uses Swashbuckle, but the setup will be very similar to NSwag. You can use the IApiVersionDescriptionProvider service to enumerate all of the API versions defined in your application. That should significantly simplify your NSwag configuration.
You're versioning by URL segment; therefore, to address Problem 3 you simply need to configure the API Explorer a la:
services.AddVersionedApiExplorer( options => options.SubstituteApiVersionInUrl = true );
This will replace the {version} route parameter in the route template with the corresponding API version value and remove the API version parameter from the API description.
I have a bunch of already written middlewares. As i know they can easily be used at global pipeline level.
[assembly: OwinStartup(typeof(Startup))]
namespace Portal.Web
{
public class Startup
{
public void Configuration(IAppBuilder builder)
{
builder
.UseApiResponseCompression()
....
}
}
}
But i really excited hypothetical opportunities to use this middleware in appropriate controller or method ( I've seen such functionality implementation in asp.net core)
Q: Is it possible to use arbitrary middleware in specific controller or ever method ( like filters) in asp.net web api2?
Will MiddlewareFilter do what you are expecting?
You create a pipeline class, say MyPipeline, which has one method with signature public void Configure(IApplicationBuilder applicationBuilder). Then place [MiddlewareFilter(typeof(MyPipeline))] on the controller or action.
I have a project using OWIN. The whole solution consists of five parts:
Web application
Core
Infrastructure
WebApi
Tests
The project works well.
Recently, I planned to put swagger in my project. After some research, I decided to use Swashbuckle. https://github.com/domaindrivendev/Swashbuckle#custom-routes
I followed its tutorial by adding the following code to my Startup.cs:
HttpConfiguration Config = new HttpConfiguration();
WebApiConfig.Register(Config);
Config.EnableSwagger((c) =>
{
c.SingleApiVersion("v1", "Flynn Forms");
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
}).EnableSwaggerUi();
app.UseWebApi(Config);
Then, I did my API documentation at http://localhost:22391/swagger/ui/index. However, all my APIs stopped working. I got errors of my controllers such as:
An error occurred when trying to create a controller of type 'FormTemplateController'. Make sure that the controller has a parameterless public constructor.
I did not have a parameterless constructor... But it worked well before adding the Swashbuckle. I googled this error, someone suggested that just adding a parameterless constructor will solve this error. So my controller became:
[RoutePrefix("api/develop")]
public class FormTemplateController : ApiController
{
private IFormTemplateService formTemplateService;
public FormTemplateController()
{
}
public FormTemplateController(IFormTemplateService formTemplateService)
{
this.formTemplateService = formTemplateService;
}
[Route("form/{formId}")]
[HttpGet]
public FormTemplateEntity GetActiveFormTemplateByformId(string formId)
{
FormTemplateEntity formTemplate = formTemplateService.GetActiveFormTemplateEntityByFormId(formId);
return formTemplate;
}
....(different APIs)
}
The error did has gone. But I am now getting a new error indicating that formTemplateService is null.
I am using Castle Windsor as Inversion of Control container for my project. Does Swashbuckle have some conflicts with Castle Windsor? Does anyone have same issue and get a solution? I am not sure if I have provided enough background information. If you need more information, please leave them in the comments.
Thank you.
After trying different methods, I found the following works.
At first, I put the codes:
HttpConfiguration Config = new HttpConfiguration();
WebApiConfig.Register(Config);
Config.EnableSwagger((c) =>
{
c.SingleApiVersion("v1", "Flynn Forms");
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
}).EnableSwaggerUi();
app.UseWebApi(Config);
in Startup.cs cause I thought this is the entrance of my solution.
Then, I notice in my WebApi part, I have a file called WebApiConfig.cs which configs the instance of HttpConfiguration as well. So I moved codes to this file. (Because there has already been an instance of HttpConfiguration there, you don't have to create it again.) Just put the following codes there:
Config.EnableSwagger((c) =>
{
c.SingleApiVersion("v1", "Flynn Forms");
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
}).EnableSwaggerUi();
The problem got solved.
I'm developing an MVC API in a separate class library. The API methods use attribute routing. The API will be used by other MVC applications (not built by me).
The main MVC application will reference my library assembly and call AddMvc() / UseMvc() in it's own startup class. It will be able to set the root API url's for my API library dynamically (from configuration or options setup delegate), so that it can make sure there are no conflicts with it's own routes, which can use either attribute routing or centralized routing.
So let's say my API library has a product/{id} route. The main application should be able to choose any route prefix, like api/product/{id} or some/other/prefix/product/{id}.
At startup, MVC will discover all controllers/routes in all referenced assemblies, and it will also discover and register my API library routes, but only on the hardcoded product/{id} route without any prefix.
I've been trying to get MVC to register the routes with a prefix, but so far no success. The main application will call custom AddMyApi() / UseMyApi() config methods, so I can do configuration / setup for my library. Some of the things I tried:
Mapping
app.Map("/custom-prefix", api =>
{
api.UseMvc();
});
This will result in duplicate routes for both custom-prefix/product/{id} and product/{id}.
Route Convention
Based on http://www.strathweb.com/2016/06/global-route-prefix-with-asp-net-core-mvc-revisited/
services.AddMvc(options =>
{
options.Conventions.Insert(0, new RouteConvention(new RouteAttribute("custom-prefix")));
});
It looks like this will not work because the options will be overwritten by the main application's call to AddMvc(), or the other way around, depending which gets called first.
Custom route attribute
A custom route attribute based on IRouteTemplateProvider on the Controller classes will not work because I need the prefix injected from an options class, and attributes do not support constructor injection.
Postpone discovery of routes
Based on http://www.strathweb.com/2015/04/asp-net-mvc-6-discovers-controllers/
I've added [NonController] to the library controllers to prevent them being discovered at the main application's startup. However I've not been able to add them later, and also I suppose I will run into the same problem of the main application overwriting the MVC options again.
Areas
I can't use areas, because the main application may decide to run the API from the root (without prefix).
So I'm stuck as to how to solve this problem. Any help is appreciated.
I believe a convention is the right approach here and the bit you are missing is just providing the proper extension method for your library to be registered within MVC.
Start by creating a convention that will add a prefix to all controllers that pass a certain selector.
It is based on one I wrote for adding culture prefixes, but the idea is very similar to the article you linked.
Basically it will either update any existing AttributeRouteModel or add a new one if none is found.
This would be an example of such a convention:
public class ApiPrefixConvention: IApplicationModelConvention
{
private readonly string prefix;
private readonly Func<ControllerModel, bool> controllerSelector;
private readonly AttributeRouteModel onlyPrefixRoute;
private readonly AttributeRouteModel fullRoute;
public ApiPrefixConvention(string prefix, Func<ControllerModel, bool> controllerSelector)
{
this.prefix = prefix;
this.controllerSelector = controllerSelector;
// Prepare AttributeRouteModel local instances, ready to be added to the controllers
// This one is meant to be combined with existing route attributes
onlyPrefixRoute = new AttributeRouteModel(new RouteAttribute(prefix));
// This one is meant to be added as the route for api controllers that do not specify any route attribute
fullRoute = new AttributeRouteModel(
new RouteAttribute("api/[controller]"));
}
public void Apply(ApplicationModel application)
{
// Loop through any controller matching our selector
foreach (var controller in application.Controllers.Where(controllerSelector))
{
// Either update existing route attributes or add a new one
if (controller.Selectors.Any(x => x.AttributeRouteModel != null))
{
AddPrefixesToExistingRoutes(controller);
}
else
{
AddNewRoute(controller);
}
}
}
private void AddPrefixesToExistingRoutes(ControllerModel controller)
{
foreach (var selectorModel in controller.Selectors.Where(x => x.AttributeRouteModel != null).ToList())
{
// Merge existing route models with the api prefix
var originalAttributeRoute = selectorModel.AttributeRouteModel;
selectorModel.AttributeRouteModel =
AttributeRouteModel.CombineAttributeRouteModel(onlyPrefixRoute, originalAttributeRoute);
}
}
private void AddNewRoute(ControllerModel controller)
{
// The controller has no route attributes, lets add a default api convention
var defaultSelector = controller.Selectors.First(s => s.AttributeRouteModel == null);
defaultSelector.AttributeRouteModel = fullRoute;
}
}
Now, if this was all part of an app you are writing instead of a library, you would just register it as:
services.AddMvc(opts =>
{
var prefixConvention = new ApiPrefixConvention("api/", (c) => c.ControllerType.Namespace == "WebApplication2.Controllers.Api");
opts.Conventions.Insert(0, prefixConvention);
});
However since you are providing a library, what you want is to provide an extension method like AddMyLibrary("some/prefix") that will take care of adding this convention and any other setup like registering required services.
So you can write an extension method for IMvcBuilder and update the MvcOptions inside that method. The nice thing is that since is an extension of IMvcBuilder, it will always be called after the default AddMvc():
public static IMvcBuilder AddMyLibrary(this IMvcBuilder builder, string prefix = "api/")
{
// instantiate the convention with the right selector for your library.
// Check for namespace, marker attribute, name pattern, whatever your prefer
var prefixConvention = new ApiPrefixConvention(prefix, (c) => c.ControllerType.Namespace == "WebApplication2.Controllers.Api");
// Insert the convention within the MVC options
builder.Services.Configure<MvcOptions>(opts => opts.Conventions.Insert(0, prefixConvention));
// perform any extra setup required by your library, like registering services
// return builder so it can be chained
return builder;
}
Then you would ask users of your library to include it within their app as in:
services.AddMvc().AddMyLibrary("my/api/prefix/");
//Try this Reference enter link description here
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UsePathBase("/Api/v/00");
app.Map("/api/v/0", api =>
{
api.UseMvc();
});
}