【发布时间】:2021-09-25 07:06:59
【问题描述】:
我正在尝试制作一个连接到您的 MS 帐户的 React 应用程序。我遇到的问题是,当没有头像的人注册时,API会将其视为错误。最好自己看看。 How it looks when there is no profile picture on the MS account
当没有图片可显示时,如何使其显示默认个人资料图片?
这是后端和请求发送。
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Graph;
namespace BursaLicentelor.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class MicrosoftGraphController : ControllerBase
{
private readonly GraphServiceClient _graphClient;
public MicrosoftGraphController(GraphServiceClient graphClient)
{
_graphClient = graphClient;
}
[HttpGet("photo/{userPrincipalName}")]
public async Task<IActionResult> GetProfilePhoto([Required] string userPrincipalName)
{
var photo = await _graphClient.Users[userPrincipalName]
.Photos["240x240"]
.Content
.Request()
.GetAsync();
return File(photo, "image/png");
}
}
}
这是图片的 React 组件:
// This is the front-end React component
import React from "react";
export default function Photo({ email }) {
return (
<img
className="profilePicture"
src={`api/microsoftgraph/photo/${email}`}
alt="avatar"
/>
);
}
【问题讨论】:
标签: c# reactjs image react-native microsoft-graph-api