Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analyzer: Warn that IResult should not be returned from Controller Actions #39692

Closed
Tracked by #33403
brunolins16 opened this issue Jan 21, 2022 · 5 comments
Closed
Tracked by #33403
Labels
analyzer Indicates an issue which is related to analyzer experience feature-mvc-execution-pipeline Features related to how MVC executes a controller or razor page ✔️ Resolution: Won't Fix Resolved because we decided not to change the behavior reported in this issue. old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels Status: Resolved

Comments

@brunolins16
Copy link
Member

brunolins16 commented Jan 21, 2022

Today, with the new Microsoft.AspNetCore.Http.Results class is very easy to get confused and implement a Controller Action similar with that, or any other variation of the methods exposed by this class:

[HttpGet]
public IResult Get()
{
    return Results.Ok();
}

Also, is very easy to use an Microsoft.AspNetCore.Http.IResult custom implementation as a return of a controller action, like this:

public class MyCustomResult : IResult
{
    public Task ExecuteAsync(HttpContext httpContext) => Task.CompletedTask;
}
[HttpGet]
public IResult Get()
{
    return new MyCustomResult();
}

Both examples will not throw exception but will serialize the class. For the first example the action will return the following payload:

{
  "value": null,
  "statusCode": 200,
  "contentType": null
}

In rare situations it will be the expected behavior. With that, we should add an analyzer that detects if the action return an Microsoft.AspNetCore.Http.IResult and fail/warn. It might be also possible provide a code fix suggestion in cases where the ControllerBase has a similar method, eg:

From:

[HttpGet]
public IResult Get()
{
    return Results.Ok();
}

To:

[HttpGet]
public IResult Get()
{
    return Ok();
}
@TanayParikh TanayParikh added the old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Jan 21, 2022
@brunolins16 brunolins16 changed the title Consider support for IResult Analyzer: Warn that IResult should not be returned from Controller Actions Jan 21, 2022
@brunolins16 brunolins16 added this to the .NET 7 Planning milestone Jan 21, 2022
@ghost
Copy link

ghost commented Jan 21, 2022

Thanks for contacting us.

We're moving this issue to the .NET 7 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@brunolins16 brunolins16 added analyzer Indicates an issue which is related to analyzer experience Cost:S Priority:0 Work that we can't release without feature-mvc-execution-pipeline Features related to how MVC executes a controller or razor page labels Jan 21, 2022
@davidfowl
Copy link
Member

Or maybe we should support them?

@brunolins16
Copy link
Member Author

@davidfowl In my opinion, it is very confusing already and will become even more when we start look into #37502. However, this is just my initial thoughts related to #33403 and maybe we support them might be a better option.

@brunolins16 brunolins16 self-assigned this Jan 25, 2022
@brunolins16 brunolins16 removed the Priority:0 Work that we can't release without label Jan 25, 2022
@brunolins16 brunolins16 removed their assignment Jan 25, 2022
@davidfowl
Copy link
Member

Yea I see how that can be very problematic from a developer POV. Also unfortunately the controller base class has all of those methods that make it hard to move away from action result and prefer Results.XX. Maybe throwing is a fine way to start.

@brunolins16 brunolins16 added the ✔️ Resolution: Won't Fix Resolved because we decided not to change the behavior reported in this issue. label Mar 21, 2022
@ghost ghost added the Status: Resolved label Mar 21, 2022
@brunolins16
Copy link
Member Author

We decided to add the support for IResult types in MVC Action controllers. #40639

@ghost ghost locked as resolved and limited conversation to collaborators Apr 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
analyzer Indicates an issue which is related to analyzer experience feature-mvc-execution-pipeline Features related to how MVC executes a controller or razor page ✔️ Resolution: Won't Fix Resolved because we decided not to change the behavior reported in this issue. old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels Status: Resolved
Projects
None yet
Development

No branches or pull requests

4 participants
@davidfowl @brunolins16 @TanayParikh and others