【发布时间】:2018-11-14 22:48:26
【问题描述】:
我希望以下 Post 方法通过将失败值分配给“result”变量来返回失败,但我不知道如何实现。理想情况下,它会说安装 ID 无效,但不确定我是否可以这样做:
[Authorize]
[HttpPost, Route("sendForDevelopment")]
public async Task<NotificationOutcome> Post([FromBody]string message, string installationId)
{
string hubName = "myHubName";
string hubNameDefaultShared = "myHubNameDefaultShared";
NotificationHubClient hub = NotificationHubClient
.CreateClientFromConnectionString(hubNameDefaultShared, hubName, enableTestSend: true);
var templateParams = new Dictionary<string, string>
{
["messageParam"] = message
};
NotificationOutcome result = null;
if (string.IsNullOrWhiteSpace(installationId))
{
// output a installation id is null or empty message or assign failure to the result variable
}
else
{
result = await hub.SendTemplateNotificationAsync(templateParams, "$InstallationId:{" + installationId + "}").ConfigureAwait(false);
}
return result;
}
【问题讨论】:
-
让操作的结果是一个
IHttpActionResult派生对象。这应该为您可以返回的内容提供更大的灵活性 -
@Nkosi 我在想那些话。如果操作的结果是 IHttpActionResult,它还会在成功的情况下包含与 NotificationOutcomeResult 相同的消息或类似的消息?
-
那么只需创建一个
NotificationOutcome的实例并根据需要进行填充 -
@Nkosi 我无法填充,我检查了 NotificationOutcome 的类定义,但没有弄清楚如何手动为其分配失败。
-
@Nkosi 我也刚尝试过 IHttpActionResult 但我在 result = await hub.SendTemplateNotificationAsync(..) 行中收到错误,说它不能将 NotificationOutcome 隐式转换为 IHttpActionResult
标签: c# asp.net-web-api push-notification