简体   繁体   English

任务的返回类型<type>在 ASP.NET 核心控制器中</type>

[英]Return Type from Task<Type> in ASP.NET CORE Controllers

I have a method in my我有一个方法

BaseController基本控制器

public async Task<BaseModel> PopulateBaseModelAsync(BaseModel m)
{
    m.CreatedDate = DateTime.Now;
    m.CreatedById = await _userManager.GetUserAsync(User);
    m.CreatedByUsername = User.Identity.Name;
    return m;
}

It's forced to be an async Task because of the _userManager.GetUserAsync method.由于_userManager.GetUserAsync方法,它被迫成为async Task

In a derived controller I want to pass in an BaseModel and have those three properties populated eg在派生的 controller 中,我想传入一个BaseModel并填充这三个属性,例如

DerivedController派生控制器

public async Task<IActionResult> CategoriesCreateAsync(BookingCategory bc)
{
    if(bc != null)
    {
        bc = (BookingCategory)PopulateBaseModelAsync(bc);
        _context.BookingCategories.Add(bc);
        _context.SaveChanges();
    }
    return RedirectToAction(nameof(CategoriesIndex));
}

I originally wanted PopulateBaseModel(BaseModel) to just return a new BaseModel eg below, but because of the forced async method I'm not sure how to achieve this.我最初希望PopulateBaseModel(BaseModel)只返回一个新的 BaseModel 例如下面,但由于强制async方法,我不知道如何实现这一点。

Original aim最初的目标

BaseController基本控制器

    public BaseModel PopulateBaseModel(BaseModel m)
    {
        m.CreatedDate = DateTime.Now;
        m.CreatedById = _userManager.GetUser(User); // Must use async :(
        m.CreatedByUsername = User.Identity.Name;
        return m;
    }

DerivedController派生控制器

    public IActionResult CategoriesCreate(BookingCategory bc)
    {
        if(bc != null)
        {
            bc = (BookingCategory)PopulateBaseModel(bc);
            _context.BookingCategories.Add(bc);
            _context.SaveChanges();
        }
        return RedirectToAction(nameof(CategoriesIndex));
    }

My main issue is I don't really understand how Task works - the method我的主要问题是我不太了解Task的工作原理 - 方法

public async Task<BaseModel> PopulateBaseModelAsync(BaseModel m)

allows me to return BaseModel but it won't compile because it can't convert Task<BaseModel> to BookingCategory允许我返回BaseModel但它不会编译,因为它无法convert Task<BaseModel> to BookingCategory

Your just missing an await :您只是缺少一个await

public async Task<IActionResult> CategoriesCreateAsync(BookingCategory bc)
{
    if(bc != null)
    {
        await PopulateBaseModelAsync(bc);
        _context.BookingCategories.Add(bc);
        _context.SaveChanges();
    }
    return RedirectToAction(nameof(CategoriesIndex));
}

Because PopulateBaseModelAsync returns a Task<IBaseModel> you need to await it's completion.因为PopulateBaseModelAsync返回一个Task<IBaseModel>你需要等待它的完成。

Here the Task represents an I/O operation, who's result isn't immediately available.这里的Task代表一个 I/O 操作,其结果不是立即可用的。

Without awaiting the Task , your method would continue before the result is obtained.无需等待Task ,您的方法将在获得结果之前继续。

No need to re-assign to bc either, because PopulateBaseModelAsync is modifying the instance it references.也不需要重新分配给bc ,因为PopulateBaseModelAsync正在修改它引用的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何返回&#39;Func类型的值 <TextWriter, Task> 在asp.net core 2.0中 - How return a value of type 'Func<TextWriter, Task>' in asp.net core 2.0 ASP.NET Core - 从 controller 类型获取路由 - ASP.NET Core - get route from controller type ASP.NET 核心 3 - 可空类型 - 从 json 中删除属性 - ASP.NET Core 3 - nullable type - removes property from json ASP.NET Core如何将任何类型转换为ActionResult <T> 控制器动作的返回类型? - How is ASP.NET Core able to convert any type to ActionResult<T> return type of controller actions? asp.net核心默认ErrorMessageResourceName和Type - asp.net core default ErrorMessageResourceName and Type 在ASP.NET Core中发布复杂类型 - Posting complex type in ASP.NET Core 使用autofac初始化类型时注册ASP.NET MVC 3控制器 - Register ASP.NET MVC 3 controllers when initializing type with autofac ASP.NET CORE:尝试激活“API.Controllers.UsersController”时无法解析“API.SQLConnection.IDBConnection”类型的服务 - ASP.NET CORE : Unable to resolve service for type 'API.SQLConnection.IDBConnection' while attempting to activate 'API.Controllers.UsersController' Asp.net核心类型不得为ByRef参数名称:type - Asp.net core Type must not be ByRef Parameter name: type 如何在 ASP.NET Core 的 OData 中导航/配置从复杂类型到实体类型的导航? - How do you navigate/configure navigation from complex type to entity type in OData in ASP.NET Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM