首页 > 解决方案 > Sendgrid 仅查找旧模板

问题描述

我正在为一个项目设置一个 SendGrid 客户端。当我尝试在 SendGrid 中访问我的模板时,它只返回旧模板,但完全忽略事务模板。我没有在网上找到任何关于这个特定问题的文档。

public async Task<Dictionary<string, string>> GetTemplateIdsByNameAsync()
    {
        //https://sendgrid.com/docs/API_Reference/Web_API_v3/Transactional_Templates/templates.html
        //My client is being autheticated succesfully in the GetSendGridClient function
        var client = GetSendGridClient();

        //This function should get all the templates connected to my SendGrid account but only finds the legacy ones
        var response = await client.RequestAsync(method: SendGrid.SendGridClient.Method.GET, urlPath:"templates");

        ThrowExceptionIfStatusIsNotOk(response);

        var content = await response.Body.ReadAsStringAsync();

        var anonymousTemplatesObject = new { Templates = new[] { new { Id = "", Name = "" } } };

        var templates = _genericJsonSerializer.DeserializeAnonymous(content, anonymousTemplatesObject);

        return templates.Templates.ToDictionary(x => x.Name, x => x.Id);
    }

我已经在这个问题上坐了一整天,但还没有找到解决方案

标签: templatessendgridtransactionallegacy

解决方案


虽然 SendGrid 尚未正确记录它,但正确的 API 调用语法是https://api.sendgrid.com/v3/templates?generations=legacy,dynamic

所以在这种情况下你需要使用https://api.sendgrid.com/v3/templates?generations=dynamic

有关更多背景信息,请参见此处:https ://github.com/sendgrid/sendgrid-csharp/issues/723


推荐阅读