Skip to content

Create IResult-returning static methods  #33729

Closed
@halter73

Description

@halter73

IResult-returning static methods would allow something like using static Microsoft.AspNetCore.Http.Results and makes for better return type inference for IResult-returning lambdas, so you don't have to do casting like the following you have to do using constructors today.

app.MapGet("/todos/{id}", async (int id) =>
{
    using var db = new TodoDbContext(options);
 
    if (await db.Todos.FindAsync(id) is not Todo todo)
    {
        return (IResult)new NotFoundResult();
    }
 
    return new JsonResult(todo);
})

With, IResult returning NotFound and Ok methods the (IResult) casting can be removed.

app.MapGet("/todos/{id}", async (int id) =>
{
    using var db = new TodoDbContext(options);
 
    if (await db.Todos.FindAsync(id) is not Todo todo)
    {
        return NotFound();
    }
 
    return Json(todo);
})

Metadata

Metadata

Assignees

Labels

area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-minimal-actionsController-like actions for endpoint routing

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions