C# ASP.NET核心3.1.1 MVC+;角度:我应该如何从用户(id)处获取所有食谱?

C# ASP.NET核心3.1.1 MVC+;角度:我应该如何从用户(id)处获取所有食谱?,c#,angular,asp.net-mvc,C#,Angular,Asp.net Mvc,我正在学习ASP.NET MVC,目前正在使用API开发ASP.NET Core 3.1.1中的单页应用程序 我有User.cs,其中有他的食谱集 在我的User类中,我有一个属性: public ICollection<Recipe> Recipes {get; set;} 我有两个控制器,RecipesController和UserController,在这两个控制器中,我可以执行GetUser(id),GetUsers(),GetRecipes(id),GetRecipes(

我正在学习ASP.NET MVC,目前正在使用API开发ASP.NET Core 3.1.1中的单页应用程序

我有
User.cs
,其中有他的
食谱集

在我的
User
类中,我有一个属性:

public ICollection<Recipe> Recipes {get; set;}
我有两个控制器,
RecipesController
UserController
,在这两个控制器中,我可以执行
GetUser(id)
GetUsers()
GetRecipes(id)
GetRecipes()

此处显示的示例是如何在控制器中获得单个用户和所有用户:

[Authorize]
[Route("api/[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
    private readonly IRecipesRepository _repository;
    private readonly IMapper _mapper;

    public UsersController(IRecipesRepository repository, IMapper mapper)
    {
        _repository = repository;
        _mapper = mapper;
    }

    [HttpGet] 
    public async Task<IActionResult> GetUsers ()
    {
        var users = await _repository.GetUsers();

        var usersToReturn = _mapper.Map<IEnumerable<UserForListDto>>(users);

        return Ok(usersToReturn);
    }

    [HttpGet("{id}")] 
    public async Task<IActionResult> GetUser (int id)
    {
        var user = await _repository.GetUser(id);

        var userToReturn = _mapper.Map<UserForDetailDto>(user);

        return Ok(userToReturn);
    }
[授权]
[路由(“api/[控制器]”)]
[ApiController]
公共类UsersController:ControllerBase
{
专用只读IRecipesRepository存储库;
专用只读IMapper\u映射器;
公共用户控制器(IRecipesRepository存储库,IMapper映射器)
{
_存储库=存储库;
_映射器=映射器;
}
[HttpGet]
公共异步任务GetUsers()
{
var users=await_repository.GetUsers();
var usersToReturn=_mapper.Map(用户);
返回Ok(usersToReturn);
}
[HttpGet(“{id}”)]
公共异步任务GetUser(int-id)
{
var user=wait_repository.GetUser(id);
var userToReturn=\u mapper.Map(用户);
返回Ok(用户返回);
}
我也想得到所有用户的食谱

我应该像
localhost:4200/users/1/recipes

我不确定我的思维方式是否好

也许我应该尝试从位于UserId的Recipe类中获取recipes


我应该收到单用户insead列表

一个解决方案是将控制器修改为:

[HttpGet("{id}/{withRecipe?}")]
public async Task<IActionResult> GetUser (int id, bool withRecipe = false)
然后回到控制器中,执行以下操作:

var user = withRecipe? _repository.GetUserWithRecipe(id) : _repository.GetUser(id); 
不确定如何使用wait关键字,但您可以改为使用“if”语句

通过重新使用相同的存储库方法/查询,而不是创建单独的方法,可以提高效率,但它给出了一个想法

另一种方法是使用以下内容创建另一个控制器方法:

[HttpGet("{id}/recipes")]
public async Task<IActionResult> GetUser (int id)
[HttpGet({id}/recipes”)]
公共异步任务GetUser(int-id)
它直接使用recipe include调用repository方法


这将为您提供所需的url。

一种解决方案是将控制器修改为:

[HttpGet("{id}/{withRecipe?}")]
public async Task<IActionResult> GetUser (int id, bool withRecipe = false)
然后回到控制器中,执行以下操作:

var user = withRecipe? _repository.GetUserWithRecipe(id) : _repository.GetUser(id); 
不确定如何使用wait关键字,但您可以改为使用“if”语句

通过重新使用相同的存储库方法/查询,而不是创建单独的方法,可以提高效率,但它给出了一个想法

另一种方法是使用以下内容创建另一个控制器方法:

[HttpGet("{id}/recipes")]
public async Task<IActionResult> GetUser (int id)
[HttpGet({id}/recipes”)]
公共异步任务GetUser(int-id)
它直接使用recipe include调用repository方法


这会按照您的要求提供url。

好的,我有一些我想要的。但一点也没有

在我的回购协议中:

 public async Task<User> GetUserWithRecipes(int id)
    {
        var userWithRecipes = await _context.Users.
        Include(r => r.Recipes)
        .FirstOrDefaultAsync(r => r.Id == id);

        return userWithRecipes;
    }
public异步任务GetUserWithRecipes(int-id)
{
var userWithRecipes=wait_context.Users。
包括(r=>r.配方)
.FirstOrDefaultAsync(r=>r.Id==Id);
返回用户与配方;
}
用户控制器

 [HttpGet("{id}/recipes")]
    public async Task<IActionResult> GetUserWithRecipes (int id)
    {
        var user = await _repository.GetUserWithRecipes(id);

        var userToReturn = _mapper.Map<UserForRecipeDto>(user);

        return Ok(userToReturn);
    }
[HttpGet({id}/recipes”)]
公共异步任务GetUserWithRecipes(int id)
{
var user=wait_repository.GetUserWithRecipes(id);
var userToReturn=\u mapper.Map(用户);
返回Ok(用户返回);
}
在《邮递员》中,我得到了很好的回复,但我没有我的照片URL。 我必须绘制地图。我做错了。但我不确定我应该如何绘制地图

我现在是怎么做的

CreateMap<User, UserForRecipeDto>(); 
CreateMap();
用户类+照片返回到下面

    public class UserForRecipeDto
{
 public ICollection<RecipeForListDto> Recipes {get; set;}
}
公共类UserForRecipeDto
{
公共ICollection配方{get;set;}
}
}

公共类用户
{
公共int Id{get;set;}
公共字符串用户名{get;set;}
公共字节[]密码哈希{get;set;}
公共字节[]密码salt{get;set;}
公共字符串{get;set;}
公共日期时间出生日期{get;set;}
公共字符串KnownAs{get;set;}
已创建公共日期时间{get;set;}
public DateTime LastActive{get;set;}
公共字符串介绍{get;set;}
公共字符串City{get;set;}
公共字符串国家{get;set;}
公共ICollection用户照片{get;set;}
公共ICollection配方{get;set;}
}
}

问题是RecipePhotos在Recipe.cs中,但我从用户那里得到了配方

当我将配方与照片联系起来时,我这样做:

CreateMap<Recipe, RecipeForListDto>()
        .ForMember(dest => dest.PhotoUrl, opt => opt
            .MapFrom(src => src.RecipePhotos
                .FirstOrDefault(p => p.IsMain).Url));
CreateMap()
.FormMember(dest=>dest.PhotoUrl,opt=>opt
.MapFrom(src=>src.RecipePhotos
.FirstOrDefault(p=>p.IsMain.Url));

但是如何将用户/id/recipephotos与recipephotos连接起来呢?

好的,我有一些我想要的。但一点也不

在我的回购协议中:

 public async Task<User> GetUserWithRecipes(int id)
    {
        var userWithRecipes = await _context.Users.
        Include(r => r.Recipes)
        .FirstOrDefaultAsync(r => r.Id == id);

        return userWithRecipes;
    }
public异步任务GetUserWithRecipes(int-id)
{
var userWithRecipes=wait_context.Users。
包括(r=>r.配方)
.FirstOrDefaultAsync(r=>r.Id==Id);
返回用户与配方;
}
用户控制器

 [HttpGet("{id}/recipes")]
    public async Task<IActionResult> GetUserWithRecipes (int id)
    {
        var user = await _repository.GetUserWithRecipes(id);

        var userToReturn = _mapper.Map<UserForRecipeDto>(user);

        return Ok(userToReturn);
    }
[HttpGet({id}/recipes”)]
公共异步任务GetUserWithRecipes(int id)
{
var user=wait_repository.GetUserWithRecipes(id);
var userToReturn=\u mapper.Map(用户);
返回Ok(用户返回);
}
在《邮递员》中,我得到了很好的回复,但我没有我的照片URL。 我必须绘制地图。我做错了。但我不确定该如何绘制地图

我现在是怎么做的

CreateMap<User, UserForRecipeDto>(); 
CreateMap();
用户类+照片返回到下面

    public class UserForRecipeDto
{
 public ICollection<RecipeForListDto> Recipes {get; set;}
}
公共类UserForRecipeDto
{
公共ICollection配方{get;set;}
}
}

公共类用户
{
公共int Id{get;set;}
公共字符串用户名{get;set;}
公共字节[]密码哈希{get;set;}
公共字节[]密码salt{