Я внедрил пользовательскую систему оповещений в свое приложение. NET 2.2, используя следующее руководство: https://www.matheus.ro/2017/12/18/how-to-create-a-simple-alert-system-using-tag-helpers-in-asp-net-core-mvc/
Это прекрасно работает, когда я запускаю его локально, однако я развернул в живой сервер Azure прошлой ночью заметил, что нет предупреждений.
В чем может быть причина этого? Я не уверен, как эффективно отладить эту проблему.
AlertsTagHelper.cs:
[HtmlTargetElement("alerts")]
public class AlertsTagHelper : TagHelper
{
private const string AlertKey = "SEPMTool.Alert";
[ViewContext]
public ViewContext ViewContext { get; set; }
protected ITempDataDictionary TempData => ViewContext.TempData;
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "div";
if (TempData[AlertKey] == null)
TempData[AlertKey] = JsonConvert.SerializeObject(new HashSet<Alert>());
var alerts = JsonConvert.DeserializeObject<ICollection<Alert>>(TempData[AlertKey].ToString());
var html = string.Empty;
foreach (var alert in alerts)
{
html += $"<div class='alert {alert.Type}' id='inner-alert' role='alert'>" +
$"<button type='button' class='close' data-dismiss='alert' aria-label='Close'>" +
$"<span aria-hidden='true'>×</span>" +
$"</button>" +
$"{alert.Message}" +
$"</div>";
}
output.Content.SetHtmlContent(html);
}
}
AlertExtensions.cs:
public static class AlertExtensions
{
private const string AlertKey = "SEPMTool.Alert";
public static void AddAlertSuccess(this Controller controller, string message)
{
var alerts = GetAlerts(controller);
alerts.Add(new Alert(message, "alert-success"));
controller.TempData[AlertKey] = JsonConvert.SerializeObject(alerts);
}
public static void AddAlertInfo(this Controller controller, string message)
{
var alerts = GetAlerts(controller);
alerts.Add(new Alert(message, "alert-info"));
controller.TempData[AlertKey] = JsonConvert.SerializeObject(alerts);
}
public static void AddAlertWarning(this Controller controller, string message)
{
var alerts = GetAlerts(controller);
alerts.Add(new Alert(message, "alert-warning"));
controller.TempData[AlertKey] = JsonConvert.SerializeObject(alerts);
}
public static void AddAlertDanger(this Controller controller, string message)
{
var alerts = GetAlerts(controller);
alerts.Add(new Alert(message, "alert-danger"));
controller.TempData[AlertKey] = JsonConvert.SerializeObject(alerts);
}
private static ICollection<Alert> GetAlerts(Controller controller)
{
if (controller.TempData[AlertKey] == null)
controller.TempData[AlertKey] = JsonConvert.SerializeObject(new HashSet<Alert>());
ICollection<Alert> alerts = JsonConvert.DeserializeObject<ICollection<Alert>>(controller.TempData[AlertKey].ToString());
if (alerts == null)
{
alerts = new HashSet<Alert>();
}
return alerts;
}
}
Alert.cs:
public class Alert
{
public string Message;
public string Type;
public Alert(string message, string type)
{
Message = message;
Type = type;
}
}
_Layout.cs html
@using Microsoft.AspNetCore.Hosting
@inject IHostingEnvironment HostingEnvironment
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
<meta name="description" content="A Software Engineering Project Management Tool by Ben Hayward">
<title>@ViewBag.Title</title>
<link href="~/assets/img/favicon.ico" rel="icon" type="image/x-icon">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Gothic+A1" rel="stylesheet">
<link href="~/dist/assets/css/theme.css" rel="stylesheet" type="text/css" media="all" />
</head>
<body>
<div class="layout layout-nav-side">
<partial name="_LoginPartial" />
<div class="main-container">
<div id="mainContent" v-cloak>
<partial name="_BreadcrumbBarPartial" />
<alerts></alerts>
@RenderBody()
</div>
</div>
</div>
<script type="text/javascript" src="~/assets/js/jquery.min.js"></script>
<script type="text/javascript" src="~/assets/js/popper.min.js"></script>
<script type="text/javascript" src="~/dist/assets/js/bootstrap.js"></script>
<script type="text/javascript" src="~/assets/js/autosize.min.js"></script>
<script type="text/javascript" src="~/assets/js/flatpickr.min.js"></script>
<script type="text/javascript" src="~/assets/js/prism.js"></script>
<script type="text/javascript" src="~/assets/js/draggable.bundle.legacy.js"></script>
<script type="text/javascript" src="~/assets/js/swap-animation.js"></script>
<script type="text/javascript" src="~/assets/js/dropzone.min.js"></script>
<script type="text/javascript" src="~/assets/js/list.min.js"></script>
<!-- Required theme scripts (Do not remove) -->
<script type="text/javascript" src="~/dist/assets/js/theme.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="~/build/site.js" asp-append-version="true"></script>
@RenderSection("Scripts", required: false)
<script>
var app = new Vue({
el: "#mainContent",
mixins: vueMixins
});
@if (HostingEnvironment.IsDevelopment())
{
@:Vue.config.devtools = true;
}
</script>
</body>
</html>
_ViewImports. cs html:
@using SEPMTool
@using SEPMTool.Models
@addTagHelper *, SEPMTool
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper "*, cloudscribe.Web.Pagination"
Триггер оповещения в контроллере:
if (await _context.SaveChangesAsync() > 0)
{
this.AddAlertSuccess($"{projectTask.TaskName} was created successfully");
return RedirectToAction("Details", new { id = projectTask.ProjectId });
}