从控制器发送匿名类型到服务

时间:2018-03-28 03:29:13

标签: asp.net asp.net-mvc

我在服务中有这样的方法:

 public Task SendAsync(string email, string subject, string message, string template)
        {
            Execute(email, subject, message, template).Wait();
            return Task.FromResult(0);
        }

我在控制器中这样称呼它:

  await _emailService.SendAsync(user.Email, "Confirm Email", callbackurl, englishFileContent);

我想要做的是创建像这样的字符串数组:

  var englishArray = new[]
                {
                    new
                    {
                         UrlTexT=  callbackurl,
                         HeaderText = "Header"
                         WelcomeText = "Welcome",
                         VerifyText = "Please verify your email address.",
                         VerifyText2 = "Verify2",
                         Footer = "Footer"
                    }
                };

并且像这样发送:

await _emailService.SendAsync(user.Email, "Confirm Email", englishArray, englishFileContent);

和服务如:

 public Task SendAsync(string email, string subject, string[] message, string template)
        { ....}

但我收到了:

  

无法转换为匿名类型:string UrlTexT,string HeaderText,   字符串WelcomeText,字符串VerifyText,字符串VerifyText2,字符串   页脚[]到字符串

我怎样才能以正确的方式做到这一点?此致

1 个答案:

答案 0 :(得分:0)

最好创建一个类而不是发送匿名对象。但是如果你不想使用类,那么你最好使用Dictionary。

Dictionary<string, object> message = new Dictionary<string, object>()
{
    { "UrlTexT", callbackurl},
    { "HeaderText", "Header"},
    { "WelcomeText", "Welcome"},
    { "VerifyText", "Please verify your email address."},
    { "VerifyText2", "Verify2" },
    { "Footer", "Footer" }
};

await _emailService.SendAsync(user.Email, "Confirm Email", message, englishFileContent);

,您的服务是

public async Task SendAsync(string email, string subject, Dictionary<string, object> message, string template)
{
    ...
}