Sharing MVC by default

At the moment I am creating many small APIs at work. Many of these projects share some of the basic logic of controllers. Is there a way to add them to the nuget package and use them during startup?
For example. e.g. adding mvc:

IApplicationBuilder app;    
....    

app.UseMvc;
app.UseBasicApiVersionController();

The idea is that we have a version endpoint in all of our microservices.
For example:
http: // url / version
return {"version": "1.0.0"}
How can I do this in the nuget package?
So all developers only need to add one line of code to add this endpoint to their microservice? We use the dotnet core.
Do not help create the nuget package yourself.

My guess for a start is something like this:

public static IApplicationBuilder UseBasicApiVersionController(this IApplicationBuilder app)
    {
      if (app == null)
        throw new ArgumentNullException("app");

      ..... // What should I do?

      return app;
    }

* Edit:
If you add a controller to the nuget package project, it will be automatically detected. But this is not the functionality that I want. I can have 10 services that need this controller. Having 1-2 services that only want a different versioning logic. For example. The client application must not have an endpoint "/ version".
That is why during startup I want to use app.UseBasicApiVersionController ();

+6
source share
1 answer

If this is just a version, you can add a general extension or piece of middleware that returns the version.

public class VersionMiddleware: OwinMiddleware
{
    private static readonly PathString Path = new PathString("/version");
    private OwinMiddleware Next;

    public VersionMiddleware(OwinMiddleware next) : base(next)
    {
        Next = next;
    }

    public async override Task Invoke(IOwinContext context)
    {

        if (!context.Request.Path.Equals(Path))
        {
            await Next.Invoke(context);
            return;
        }

        var version = Assembly.[HowToGetYourVersionAsAnObject]

        context.Response.StatusCode = (int)HttpStatusCode.OK;
        context.Response.ContentType = "application/json";
        context.Response.Write(JsonConvert.SerializeObject(responseData));
    }
}
0
source

All Articles