根据c#中的值执行不同的代码段



我有一段c#代码,它对Azure的HTTP触发器做出反应并被执行。这段代码发送一封包含警告的电子邮件:

#r "Newtonsoft.Json"
#r "SendGrid"
using System;
using SendGrid.Helpers.Mail;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
public static SendGridMessage Run(HttpRequest req, ILogger log)
{
string requestBody = new StreamReader(req.Body).ReadToEnd();
log.LogInformation(requestBody);
var notifications = JsonConvert.DeserializeObject<IList<Notification>>(requestBody);
SendGridMessage message = new SendGridMessage();
message.Subject = "Sensor Anomaly Warning";
var content = "The following device has started registering anomalies:<br/><br/><table><tr><th>time</th><th>value</th><th>lower bound</th><th>upper bound</th><th>device</th></tr>";
foreach(var notification in notifications) {
log.LogInformation($" -  time: {notification.time}, value: {notification.value}, lowerbound: {notification.lowerbound}, upperbound: {notification.upperbound}, device: {notification.device}");
content += $"<tr><td>{notification.time}</td><td>{notification.value}</td><td>{notification.lowerbound}</td><td>{notification.upperbound}</td><td>{notification.device}</td></tr>";
}
content += "</table>";
message.AddContent("text/html", content);  
return message;
}
public class Notification
{
public string time { get; set; }
public string value { get; set; }
public string lowerbound { get; set; }
public string upperbound { get; set; }
public string device { get; set; }
}

现在,我想执行发送电子邮件的同一段代码,但是基于notification.alert的值,如果异常开始,则存储0,如果警报停止,则存储1。在python中,这就像设置"if else"一样简单。语句,在每种情况下都调用该函数。

然而对于这段c#代码,没有函数可以调用。这段代码发送了一封电子邮件,但它只是创建了一个类。无论如何,我想知道我是否可以使用"if else"c#中基于notification.alert值的语句,在一种情况下会发送一封电子邮件,表示"设备已开始注册异常"。而在另一种情况下,设备已经停止记录异常情况。我只是不能这样做,因为我必须处理notification对象,它已经在类中了。

我必须说,我不是c#开发人员,而是Python开发人员,因此有疑问。

您可以使用if/else语句或切换到基于notification.alert更新邮件正文。这个函数不发送电子邮件,它只是为电子邮件正文文本设置一个消息。

using System;
using SendGrid.Helpers.Mail;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
public static SendGridMessage Run(HttpRequest req, ILogger log)
{
string requestBody = new StreamReader(req.Body).ReadToEnd();
log.LogInformation(requestBody);
var notifications = JsonConvert.DeserializeObject<IList<Notification>>(requestBody);
SendGridMessage message = new SendGridMessage();
message.Subject = "Sensor Anomaly Warning";
var content = "The following device has started registering anomalies:   <br/><br/><table><tr><th>time</th><th>value</th><th>lower bound</th><th>upper bound</th><th>device</th></tr>";
foreach(var notification in notifications) {
log.LogInformation($" -  time: {notification.time}, value: {notification.value}, lowerbound: {notification.lowerbound}, upperbound: {notification.upperbound}, device: {notification.device}");
content += notification.alert == 0 ? "the device has started registering anomalies" : "the device has stopped registering anomalies";
}
content += "</table>";
message.AddContent("text/html", content);  
return message;
}
public class Notification
{
public string time { get; set; }
public string value { get; set; }
public string lowerbound { get; set; }
public string upperbound { get; set; }
public string device { get; set; }
public int alert { get; set;}
}

相关内容

最新更新