NToastNotify
8.0.0
服务器端 toast 通知渲染。
AJAX 调用上的 Toast 通知。 XMLHTTPRequests - 完全支持。 fetch API - 部分支持(参见示例)。
支持功能文件夹项目结构。
支持多个客户端库:toastr.js 和 noty.js。可以轻松扩展以支持更多。
诺蒂|来源
烤面包|来源
Visual Studio Nuget 包管理器 - Install-Package NToastNotify
dotnet CLI - dotnet add package NToastNotify
IMVCBuilder
或IMVCCoreBuilder
上的扩展方法using NToastNotify.Libraries;services.AddMvc().AddNToastNotifyToastr(new ToastrOptions(){ProgressBar = false,PositionClass = ToastPositions.BottomCenter});//或者直接去 services.AddMvc().AddNToastNotifyToastr();
using NToastNotify.Libraries;services.AddMvc().AddFeatureFolders().AddNToastNotifyNoty(new NotyOptions {ProgressBar = true,Timeout = 5000,Theme = "mint"});//或者简单地 goservices.AddMvc().AddNToastNotifyNoty();
注意:确保您有必要的 using 语句。
ToastrOption 参数充当 toast 库的全局选项。如果未提供任何选项,则全局设置将是默认的 toastr 选项。
公共无效配置(IApplicationBuilder 应用程序,IHostingEnvironment env) {//注意此行必须位于 .UseMvc() line.app.UseNToastNotify();app.UseMvc(routes =>{routes.MapRoute(name: "default",template: "{controller=Home}/{action =索引}/{id?}");}); }
@await Component.InvokeAsync("NToastNotify")
上面的行渲染了视图组件所需的视图。尽管您可以将此行放置在head
或body
标记内的任何位置,但建议您将此行放置在结束body
标记之前的末尾。
public class HomeController : Controller{private readonly IToastNotification _toastNotification;public HomeController(IToastNotification toastNotification){_toastNotification = toastNotification;}public IActionResult Index(){//测试默认方法//Success_toastNotification.AddSuccessToastMessage("成功消息相同");//使用默认选项成功(考虑到初始化时覆盖的默认值) Startup.cs)_toastNotification.AddSuccessToastMessage();//Info_toastNotification.AddInfoToastMessage();//Warning_toastNotification.AddWarningToastMessage();//Error_toastNotification.AddErrorToastMessage();return View();}public IActionResult About(){_toastNotification.AddInfoToastMessage( “你被重定向了”);return View();}public IActionResult Contact(){_toastNotification.AddAlertToastMessage("您将被重定向");return RedirectToAction("关于");}public IActionResult Error(){_toastNotification.AddErrorToastMessage("此请求出现问题.");return View();}public IActionResult Empty(){return View();}public IActionResult Ajax(){_toastNotification.AddInfoToastMessage("此页面将发出ajax请求并显示通知。");return View();}public IActionResult AjaxCall(){System.Threading.Thread.Sleep(2000);_toastNotification.AddSuccessToastMessage("此 toast 在 Ajax 请求时显示。 AJAX 调用 " + DateTime.Now.ToLongTimeString());return PartialView("_PartialView", "Ajax 调用");}public IActionResult NormalAjaxCall(){return PartialView("_PartialView", "普通 Ajax 调用");}public IActionResult ErrorAjaxCall(){抛出新的异常(“发生错误”);}}
public class HomeController : Controller{private readonly IToastNotification _toastNotification;public HomeController(IToastNotification toastNotification){_toastNotification = toastNotification;}public IActionResult Index(){_toastNotification.AddSuccessToastMessage();_toastNotification.AddErrorToastMessage("测试错误", new NotyOptions(){Timeout = 0});返回视图();}公共IActionResult About(){ViewData["Message"] = "您的应用程序描述页面。";_toastNotification.AddAlertToastMessage("我的关于警告消息");return View();}public IActionResult Contact(){ViewData["Message"] = "您的联系页面。";_toastNotification.AddInfoToastMessage("不要混淆。<br /> <strong>您已从联系页面重定向。<strong/>");return RedirectToAction("关于");}public IActionResult Error(){_toastNotification.AddErrorToastMessage("此请求出现问题。");return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier } );}public IActionResult Ajax(){_toastNotification.AddInfoToastMessage("该页面将发出ajax请求并显示通知。");return View();}public IActionResult AjaxCall(){System.Threading.Thread.Sleep(2000);_toastNotification.AddSuccessToastMessage("此 toast 在 Ajax 请求上显示。 AJAX 调用 " + DateTime.Now.ToLongTimeString());return PartialView("_PartialView", "Ajax 调用");}public IActionResult NormalAjaxCall(){return PartialView("_PartialView", "普通 Ajax 调用");}public IActionResult ErrorAjaxCall(){抛出新的异常(“发生错误”);}}
重定向后未显示 Toast
修复如果您使用 CookieTempDataProvider(这是默认设置),您需要接受 cookie 策略提示。
npm install
npm build
dotnet restore
dotnet build
使用dotnet run
从示例项目目录运行任何示例项目