()
23 | .Build();
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "iisSettings": {
3 | "windowsAuthentication": false,
4 | "anonymousAuthentication": true,
5 | "iisExpress": {
6 | "applicationUrl": "http://localhost:57729/",
7 | "sslPort": 0
8 | }
9 | },
10 | "profiles": {
11 | "IIS Express": {
12 | "commandName": "IISExpress",
13 | "launchBrowser": true,
14 | "environmentVariables": {
15 | "ASPNETCORE_ENVIRONMENT": "Development"
16 | }
17 | },
18 | "HeroicSamples.BootstrapAlerts": {
19 | "commandName": "Project",
20 | "launchBrowser": true,
21 | "environmentVariables": {
22 | "ASPNETCORE_ENVIRONMENT": "Development"
23 | },
24 | "applicationUrl": "http://localhost:57730/"
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/Startup.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading.Tasks;
5 | using Microsoft.AspNetCore.Builder;
6 | using Microsoft.AspNetCore.Hosting;
7 | using Microsoft.Extensions.Configuration;
8 | using Microsoft.Extensions.DependencyInjection;
9 |
10 | namespace HeroicSamples.BootstrapAlerts
11 | {
12 | public class Startup
13 | {
14 | public Startup(IConfiguration configuration)
15 | {
16 | Configuration = configuration;
17 | }
18 |
19 | public IConfiguration Configuration { get; }
20 |
21 | // This method gets called by the runtime. Use this method to add services to the container.
22 | public void ConfigureServices(IServiceCollection services)
23 | {
24 | services.AddMvc();
25 | }
26 |
27 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
28 | public void Configure(IApplicationBuilder app, IHostingEnvironment env)
29 | {
30 | if (env.IsDevelopment())
31 | {
32 | app.UseDeveloperExceptionPage();
33 | app.UseBrowserLink();
34 | }
35 | else
36 | {
37 | app.UseExceptionHandler("/Home/Error");
38 | }
39 |
40 | app.UseStaticFiles();
41 |
42 | app.UseMvc(routes =>
43 | {
44 | routes.MapRoute(
45 | name: "default",
46 | template: "{controller=Home}/{action=Index}/{id?}");
47 | });
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/Views/Home/About.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | ViewData["Title"] = "About";
3 | }
4 | @ViewData["Title"]
5 | @ViewData["Message"]
6 |
7 | Use this area to provide additional information.
8 |
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/Views/Home/ApiDemo.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | ViewBag.Title = "API Demo";
3 | }
4 |
5 | API Demo
6 | Click the buttons below to call API endpoints that return status messages.
7 |
8 |
9 | Call Successful API
10 |
11 | Call Error API
12 |
13 |
14 |
15 |
16 |
17 | @section Scripts {
18 |
34 | }
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/Views/Home/Contact.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | ViewData["Title"] = "Contact";
3 | }
4 | @ViewData["Title"]
5 | @ViewData["Message"]
6 |
7 |
8 | One Microsoft Way
9 | Redmond, WA 98052-6399
10 | P:
11 | 425.555.0100
12 |
13 |
14 |
15 | Support: Support@example.com
16 | Marketing: Marketing@example.com
17 |
18 |
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/Views/Home/Index.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | ViewData["Title"] = "Home Page";
3 | }
4 |
5 |
6 |
Heroic Bootstrap Alerts
7 |
8 | This sample shows one technique of combining Bootstrap Alerts with ASP.NET Core. You can read more about the technique
9 | at try-catch-FAIL .
10 |
11 |
12 |
13 | Examples
14 |
15 |
16 |
View Result
17 |
You can show an alert off a view result like so:
18 |
public IActionResult About()
19 | {
20 | ViewData["Message"] = "Your application description page.";
21 |
22 | return View().WithSuccess("It worked!", "You were able to view the about page, congrats!");
23 | }
24 |
Try it
25 |
26 |
27 |
28 |
Redirect Result
29 |
You can also show an alert off a redirect result:
30 |
public IActionResult GoHome()
31 | {
32 | return RedirectToAction("Index").WithWarning("You were redirected!", "The action you hit has bounced you back to Index!");
33 | }
34 |
Try it
35 |
36 |
37 |
38 |
API Result
39 |
Returning data from a Web API action? You can add a status message to that, too!
40 |
public IActionResult GetWidget()
41 | {
42 | return Ok(new Widget { Name = "Test Widget"}).WithSuccess("Widget Retrieved!", "This is the widget you are looking for!");
43 | }
44 |
Try it
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/Views/Shared/Error.cshtml:
--------------------------------------------------------------------------------
1 | @model ErrorViewModel
2 | @{
3 | ViewData["Title"] = "Error";
4 | }
5 |
6 | Error.
7 | An error occurred while processing your request.
8 |
9 | @if (Model.ShowRequestId)
10 | {
11 |
12 | Request ID: @Model.RequestId
13 |
14 | }
15 |
16 | Development Mode
17 |
18 | Swapping to Development environment will display more detailed information about the error that occurred.
19 |
20 |
21 | Development environment should not be enabled in deployed applications , as it can result in sensitive information from exceptions being displayed to end users. For local debugging, development environment can be enabled by setting the ASPNETCORE_ENVIRONMENT environment variable to Development , and restarting the application.
22 |
23 |
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/Views/Shared/_Layout.cshtml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | @ViewData["Title"] - HeroicSamples.BootstrapAlerts
7 |
8 |
9 |
10 |
11 |
12 |
13 |
16 |
17 |
18 |
19 |
20 |
21 |
39 |
40 |
41 | @Html.Partial("_StatusMessages")
42 | @RenderBody()
43 |
44 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
61 |
67 |
68 |
69 |
70 | @RenderSection("Scripts", required: false)
71 |
72 |
73 |
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/Views/Shared/_StatusMessages.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | var type = (string)TempData["_alert.type"];
3 | var title = (string)TempData["_alert.title"];
4 | var body = (string)TempData["_alert.body"];
5 | }
6 |
7 |
8 | @if (!string.IsNullOrEmpty(type))
9 | {
10 |
11 | ×
12 | @title @body
13 |
14 | }
15 |
16 |
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/Views/Shared/_ValidationScriptsPartial.cshtml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
12 |
18 |
19 |
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/Views/_ViewImports.cshtml:
--------------------------------------------------------------------------------
1 | @using HeroicSamples.BootstrapAlerts
2 | @using HeroicSamples.BootstrapAlerts.Models
3 | @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
4 |
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/Views/_ViewStart.cshtml:
--------------------------------------------------------------------------------
1 | @{
2 | Layout = "_Layout";
3 | }
4 |
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "IncludeScopes": false,
4 | "LogLevel": {
5 | "Default": "Debug",
6 | "System": "Information",
7 | "Microsoft": "Information"
8 | }
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "IncludeScopes": false,
4 | "LogLevel": {
5 | "Default": "Warning"
6 | }
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/bower.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "asp.net",
3 | "private": true,
4 | "dependencies": {
5 | "bootstrap": "3.3.7",
6 | "jquery": "2.2.0",
7 | "jquery-validation": "1.14.0",
8 | "jquery-validation-unobtrusive": "3.2.6"
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/bundleconfig.json:
--------------------------------------------------------------------------------
1 | // Configure bundling and minification for the project.
2 | // More info at https://go.microsoft.com/fwlink/?LinkId=808241
3 | [
4 | {
5 | "outputFileName": "wwwroot/css/site.min.css",
6 | // An array of relative input file paths. Globbing patterns supported
7 | "inputFiles": [
8 | "wwwroot/css/site.css"
9 | ]
10 | },
11 | {
12 | "outputFileName": "wwwroot/js/site.min.js",
13 | "inputFiles": [
14 | "wwwroot/js/site.js"
15 | ],
16 | // Optionally specify minification options
17 | "minify": {
18 | "enabled": true,
19 | "renameLocals": true
20 | },
21 | // Optionally generate .map file
22 | "sourceMap": false
23 | }
24 | ]
25 |
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/wwwroot/css/site.css:
--------------------------------------------------------------------------------
1 | body {
2 | padding-top: 50px;
3 | padding-bottom: 20px;
4 | }
5 |
6 | /* Wrapping element */
7 | /* Set some basic padding to keep content from hitting the edges */
8 | .body-content {
9 | padding-left: 15px;
10 | padding-right: 15px;
11 | }
12 |
13 | /* Carousel */
14 | .carousel-caption p {
15 | font-size: 20px;
16 | line-height: 1.4;
17 | }
18 |
19 | /* Make .svg files in the carousel display properly in older browsers */
20 | .carousel-inner .item img[src$=".svg"] {
21 | width: 100%;
22 | }
23 |
24 | /* QR code generator */
25 | #qrCode {
26 | margin: 15px;
27 | }
28 |
29 | /* Hide/rearrange for smaller screens */
30 | @media screen and (max-width: 767px) {
31 | /* Hide captions */
32 | .carousel-caption {
33 | display: none;
34 | }
35 | }
36 |
37 | .margin-bottom {
38 | margin-bottom: 20px;
39 | }
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/wwwroot/css/site.min.css:
--------------------------------------------------------------------------------
1 | body{padding-top:50px;padding-bottom:20px}.body-content{padding-left:15px;padding-right:15px}.carousel-caption p{font-size:20px;line-height:1.4}.carousel-inner .item img[src$=".svg"]{width:100%}#qrCode{margin:15px}@media screen and (max-width:767px){.carousel-caption{display:none}}.margin-bottom{margin-bottom:20px}
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/wwwroot/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MattHoneycutt/HeroicSamples.BootstrapAlerts/592ab3b69aef9a56d8d010ef12717bd69332387b/HeroicSamples.BootstrapAlerts/wwwroot/favicon.ico
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/wwwroot/images/banner1.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/wwwroot/images/banner2.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/wwwroot/images/banner3.svg:
--------------------------------------------------------------------------------
1 | banner3b
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/wwwroot/images/banner4.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/wwwroot/js/site.js:
--------------------------------------------------------------------------------
1 | (function() {
2 | function showAlert(alert) {
3 | const alertContainer = $('.alert-container');
4 |
5 | const alertElement = $(`` +
6 | '× ' +
7 | `${alert.title} ${alert.body}` +
8 | '
');
9 |
10 | alertContainer.append(alertElement);
11 | alertElement.alert();
12 | }
13 |
14 | $(document).ajaxComplete((event, xhr) => {
15 | if (xhr.getResponseHeader('x-alert-type')) {
16 | const alert = {
17 | type: xhr.getResponseHeader('x-alert-type'),
18 | title: xhr.getResponseHeader('x-alert-title'),
19 | body: xhr.getResponseHeader('x-alert-body')
20 | }
21 |
22 | showAlert(alert);
23 | }
24 | });
25 | })();
26 |
--------------------------------------------------------------------------------
/HeroicSamples.BootstrapAlerts/wwwroot/js/site.min.js:
--------------------------------------------------------------------------------
1 | (function(){function n(n){const i=$(".alert-container"),t=$(``+'
×<\/span><\/button>'+`${n.title} ${n.body}`+"<\/div>");i.append(t);t.alert()}$(document).ajaxComplete((t,i)=>{if(i.getResponseHeader("x-alert-type")){const t={type:i.getResponseHeader("x-alert-type"),title:i.getResponseHeader("x-alert-title"),body:i.getResponseHeader("x-alert-body")};n(t)}})})();
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Heroic Bootstrap Alerts Sample
2 | This sample shows one technique of combining Bootstrap Alerts with ASP.NET Core. You can read more about the technique at try-catch-FAIL .
3 |
4 | ## Examples
5 |
6 | You can show an alert off a view result like so:
7 |
8 | ```cs
9 | public IActionResult About()
10 | {
11 | ViewData["Message"] = "Your application description page.";
12 |
13 | return View().WithSuccess("It worked!", "You were able to view the about page, congrats!");
14 | }
15 | ```
16 |
17 | You can also show an alert off a redirect result:
18 |
19 | ```cs
20 | public IActionResult GoHome()
21 | {
22 | return RedirectToAction("Index").WithWarning("You were redirected!", "The action you hit has bounced you back to Index!");
23 | }
24 | ```
25 |
26 | API results are supported, too:
27 |
28 | ```cs
29 | public IActionResult Success()
30 | {
31 | return Ok(DateTime.UtcNow.ToString()).WithSuccess("Success!", "The API call worked!");
32 | }
33 | ```
--------------------------------------------------------------------------------