Nswag: Generating ASP.NET Core Controllers

Created on 16 Aug 2017  路  10Comments  路  Source: RicoSuter/NSwag

SwaggerToCSharpWebApiControllerGenerator currently generates Web API 2 controllers. Would you be willing to accept a PR to generate ASP.NET Core controllers?

I'm thinking this could be handled by passing a parameter, similar to #884.

What I'm open-minded about is whether to have separate T4 templates for ASP.NET core or to just add conditional formatting based on the model.

help wanted NSwag.CodeGeneration.CSharp (Controllers) enhancement

Most helpful comment

That's pretty much it. It's minor enough.

All 10 comments

I think this is a good idea. We should use the same template as it will be much more time consuming to always update both templates with new features... What do we have to change? Just change the namespaces of the base class and attributes?

That's pretty much it. It's minor enough.

This would be a great feature. I use it for .NET core client generation right now but it would be excellent to also use it as a generator for the controllers. Then the producers and consumers could agree on one OpenAPI spec file and generate code on both sides. The perfect setup.

I created a powershell script and generated the controller. In the end of the script i just ran:

$generatedSourceFile = "mygeneratedfile.cs"

$replacements = @{
    "System.Web.Http.ApiController" = "Microsoft.AspNetCore.Mvc.Controller"; 
    "System\.Web\.Http\." = "Microsoft.AspNetCore.Mvc."; 
}

$content = [System.IO.File]::ReadAllText($generatedSourceFile)

foreach ($replacement in $replacements.GetEnumerator()) {
    $content = [System.Text.RegularExpressions.Regex]::Replace($content, $replacement.Name, $replacement.Value)
}

[System.IO.File]::WriteAllText($sourceFile, $content)

And then it worked fine for ASP.NET Core 2.0 at least

Looks like we need two new settings:

  • [ ] AspNetNamespace (default: "System.Web.Http"), use for all attributes, etc. OR

    • ~Or just IsAspNetCore and hardcode the used namespace in the template (I prefer this one because it is also used in the WebApiToSwaggerGeneratorSettings in the same way)~

  • [x] ControllerBaseClass (default: "System.Web.Http.ApiController"). Does this already exist? => Yes (BaseClass)

Hi @RSuter , wondering if you have an ETA for resolution of this issue? I can see there has been a lot of recent activity on NSwag, so I'm sure you're quite busy with other issues. This one happens to be high-priority for one of our clients. Any updates appreciated. Thanks!

@tedepstein I'm currently concentrating on other issues which are relevant for a bigger audience and my paying clients

OK @RSuter . We may take a look at this and consider submitting a pull request if it's something we can address.

It seems something changed, see https://github.com/RSuter/NSwag/issues/1564

Was this page helpful?
0 / 5 - 0 ratings