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.

Generic Type parameter of ActionResult doesn't enforce strict type.

See original GitHub issue

Describe the bug

When you set a Type on the Generic Parameter of ActionResult and return any type other than that - it would still proceed. I mean, should this be expected? or not? or am I implementing web APIs incorrectly?

To Reproduce

  1. dotnet new webapi
  2. Create a controller with a method that ActionResult is used with a generic parameter and return a different type.
class Sample {}

[HttpGet("{id}")]
public ActionResult<Sample> GetCourt(int id)
{
    return Ok(1);
}

Exceptions (if any)

Further technical details

  • ASP.NET Core version - 5.0 but this actually happens on different versions
  • Include the output of dotnet --info - down below
  • The IDE (VS / VS Code/ VS4Mac) you’re running on, and it’s version - VS19

dotnet info:

.NET SDK (reflecting any global.json):
 Version:   5.0.100
 Commit:    5044b93829

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.18363
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.100\

Host (useful for support):
  Version: 5.0.0
  Commit:  cf258a14b7

.NET SDKs installed:
  5.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
davidfowlcommented, Nov 27, 2020

We should try out best to improve the analyzer to fix this. If we can catch simple enough cases

1reaction
javiercncommented, Nov 27, 2020

@javiercn thanks for contacting us.

I believe the that ActionResult<T> is only used for the purposes of being able to express the return type on the method signature.

Other action results convert implicitly to ActionResult<T> so I believe the behavior is by design and we don’t intend to enforce it, since we want to be able to let you return alternative object types in some cases, like when there is an error.

/cc: @pranavkm can confirm.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ASP.net Core API returning ActionResult<T> not enforcing ...
1 Answer 1 · 1.The Ok method returns type of OkObjectResult : // Summary: // A base class for an MVC controller without...
Read more >
IActionResult and ActionResult - ASP.NET Core Demystified
Let's learn how to use IActionResult and ActionResult to return actions from controllers in ASP.NET Core!
Read more >
Controller action return types in ASP.NET Core web API
The preceding action accepts no parameters, so parameter constraints validation isn't needed. When multiple return types are possible, it's ...
Read more >
Constraints on type parameters - C# Programming Guide
Type parameters as constraints​​ Object . Use type parameters as constraints on generic classes in scenarios in which you want to enforce an ......
Read more >
Fix: The non-generic type 'IActionResult' cannot be used with ...
interface Microsoft.AspNetCore.Mvc.IActionResult Defines a contract that represents the result of an action method. CS0308: The non- generic ...
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