question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SwaggerToCSharpControllerGenerator: Generate Task<ActionResult<T>> for ASP.NET Core

See original GitHub issue

Please add an ability to generate controller actions that return ActionResult<T>/Task<ActionResult<T>> for ASP.NET Core.

Ref: #1386

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
optuuttilacommented, Feb 5, 2019

I actually played around this a bit and came up with this fork: https://github.com/optuuttila/NSwag/commit/cef9ecc1693bde731a636c52173562a832e3ef6a

It results in following kind of generated code: Interface: System.Threading.Tasks.Task<Microsoft.AspNetCore.Mvc.ActionResult<MyModel>> MyMethodAsync(string param1, string param2);

Controller: public System.Threading.Tasks.Task<Microsoft.AspNetCore.Mvc.ActionResult<MyModel>> MyMethod(string param1, string param2) { return _implementation.MyMethodAsync(param1, param2); } Obviously the code would need to be finalized (a PR for the NJsonSchema.CodeGeneration.CSharp package), but before doing any of that I just wanted to ask whether this is anything along the lines you would accept as an PR?

1reaction
altsocommented, Aug 22, 2018

I believe it’s the same as with System.Net.Http.HttpResponseMessage for ASP.NET WebAPI which is allowed right now (https://github.com/RSuter/NSwag/blob/master/src/NSwag.CodeGeneration.CSharp/Templates/Controller.liquid#L94).

It gives you a better flexibility around http status codes.

[HttpPut("resource/{id}")]
public async Task<ActionResult<Resource>> UpdateResource(int id, [FromBody] Resource resource)
{
    if (/* resource exists */)
    {
        if (/* custom validation (e.g. check for fields that could not be updated) */)
        {
            if (/* user is permitted to update this specific resource */)
            {
                // update resource and return an updated copy (e.g. last modified date field)
                var updatedResource = _resourceService.Update(resource);
                return Ok(updatedResource);
            }

            return Forbid();
        }

        return BadRequest();
    }

    return NotFound();
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Controller action return types in ASP.NET Core web API
This attribute produces more descriptive response details for web API help pages generated by tools like Swagger. [ProducesResponseType] ...
Read more >
The Most Complete ASP.NET Core UI Component Library
NET Core UI Component Library. Create modern cross-platform web applications with over 110+ full-featured ASP.NET Core UI components for any scenario.
Read more >
Making an ASP.NET Core Website | ASP.NET Core 101 [2 of 13]
Learn more ➡️ https://learn.microsoft.com/training/paths/ aspnet - core -web-app/ Creating a dynamic website is easier than you might thing.
Read more >
Build and run an ASP.NET Core app in a container
Develop, build, and debug an ASP.NET Core app in a Docker container, using Visual Studio Code.
Read more >
Deploying an ASP.NET core application with Elastic Beanstalk
First, you will use the .NET Core SDK's dotnet command line tool to generate a basic .NET Core command line application, install dependencies,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found