Code snippets for both /2016/12/03/application-types/ and /2016/12/06/model-view-controller/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>@ViewData["Title"]</title> | |
<environment names="Development"> | |
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" /> | |
<link rel="stylesheet" href="~/css/site.css" /> | |
</environment> | |
</head> | |
<body> | |
<div class="navbar navbar-inverse navbar-fixed-top"> | |
<div class="container"> | |
<div class="navbar-header"> | |
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> | |
<span class="sr-only">Toggle navigation</span> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
</button> | |
<a asp-area="" asp-controller="Home" asp-action="Index" class="navbar-brand">Ok</a> | |
</div> | |
<div class="navbar-collapse collapse"> | |
<ul class="nav navbar-nav"> | |
<li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li> | |
<li><a asp-area="" asp-controller="Home" asp-action="About">About</a></li> | |
<li><a asp-area="" asp-controller="Home" asp-action="Contact">Contact</a></li> | |
</ul> | |
@await Html.PartialAsync("_LoginPartial") | |
</div> | |
</div> | |
</div> | |
<div class="container body-content"> | |
@RenderBody() | |
<hr /> | |
<footer> | |
<p>© 2016 - Ok</p> | |
</footer> | |
</div> | |
<environment names="Development"> | |
<script src="~/lib/jquery/dist/jquery.js"></script> | |
<script src="~/lib/bootstrap/dist/js/bootstrap.js"></script> | |
<script src="~/js/site.js" asp-append-version="true"></script> | |
</environment> | |
@RenderSection("scripts", required: false) | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Mvc; | |
using WebMvcApplication.ViewModels; | |
using System; | |
namespace WebMvcApplication.Controllers | |
{ | |
public class HomeController : Controller | |
{ | |
public IActionResult Index() | |
{ | |
return View(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Mvc; | |
using WebMvcApplication.ViewModels; | |
using WebMvcApplication.Services; | |
using System; | |
namespace WebMvcApplication.Controllers | |
{ | |
public class ProductController : Controller | |
{ | |
private readonly IProductService _productService { get; set; } | |
public class ProductController(IProductService productService) | |
{ | |
_productService = productService | |
} | |
public IActionResult Information(int id) | |
{ | |
// _productService queries the Products database | |
var productViewModel = _productService.getOrCreate(id); | |
return View(productViewModel); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Mvc; | |
using WebMvcApplication.ViewModels; | |
using System; | |
namespace WebMvcApplication.Controllers | |
{ | |
public class HomeController : Controller | |
{ | |
public IActionResult Index() | |
{ | |
// Create the best view model ever | |
var viewModel = new SimpleViewModel | |
{ | |
Message = "This is a simple message to the visitor", | |
HitCounter = 1000000; | |
}; | |
// Pass the new view model into the view | |
return View(viewModel); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@using Microsoft.AspNetCore.Http | |
@using Microsoft.AspNetCore.Http.Authentication | |
// We don't have a view model here | |
@{ | |
ViewData["Title"] = "Homepage"; | |
} | |
<h2>This is not the view you are looking for</h2> | |
<div class="row"> | |
<div class="col-md-8 col-md-offset-2"> | |
<p>At a space port in a galaxy far, far away</p> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@using Microsoft.AspNetCore.Http | |
@using Microsoft.AspNetCore.Http.Authentication | |
// Inform Razor of the model type to use | |
@model WebMvcApplication.ViewModels.SimpleViewModel | |
@{ | |
ViewData["Title"] = "Homepage"; | |
} | |
<h2>@Model.Message</h2> | |
<div class="row"> | |
<div class="col-md-8 col-md-offset-2"> | |
<p>We've had over @Model.HitCounter hits so far!</p> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
yo aspnet | |
cd WebMvcApplication | |
dotnet restore | |
dotnet build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Homepage</title> | |
<link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.6/css/bootstrap.min.css" /> | |
<meta name="x-stylesheet-fallback-test" content="" class="sr-only" /><script>!function(a,b,c){var d,e=document,f=e.getElementsByTagName("SCRIPT"),g=f[f.length-1].previousElementSibling,h=e.defaultView&&e.defaultView.getComputedStyle?e.defaultView.getComputedStyle(g):g.currentStyle;if(h&&h[a]!==b)for(d=0;d<c.length;d++)e.write('<link rel="stylesheet" href="'+c[d]+'"/>')}("position","absolute",["\/lib\/bootstrap\/dist\/css\/bootstrap.min.css"]);</script> | |
<link rel="stylesheet" href="/css/site.min.css?v=G7OG5flN0NdPJ13sNYOv3Hwkc-gAxRfBTYgtu6Sl0yk" /> | |
</head> | |
<body> | |
<div class="navbar navbar-inverse navbar-fixed-top"> | |
<div class="container"> | |
<div class="navbar-header"> | |
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> | |
<span class="sr-only">Toggle navigation</span> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
</button> | |
<a class="navbar-brand" href="/">Ok</a> | |
</div> | |
<div class="navbar-collapse collapse"> | |
<ul class="nav navbar-nav"> | |
<li><a href="/">Home</a></li> | |
<li><a href="/Home/About">About</a></li> | |
<li><a href="/Home/Contact">Contact</a></li> | |
</ul> | |
<ul class="nav navbar-nav navbar-right"> | |
<li><a href="/Account/Register">Register</a></li> | |
<li><a href="/Account/Login">Log in</a></li> | |
</ul> | |
</div> | |
</div> | |
</div> | |
<div class="container body-content"> | |
<h2>This is a simple message to the visitor</h2> | |
<div class="row"> | |
<div class="col-md-8 col-md-offset-2"> | |
<p>We've had over 1000000 hits so far!</p> | |
</div> | |
</div> | |
<hr /> | |
<footer> | |
<p>© 2016</p> | |
</footer> | |
</div> | |
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-2.2.3.min.js"></script> | |
<script>(window.jQuery||document.write("\u003Cscript src=\u0022\/lib\/jquery\/dist\/jquery.min.js\u0022\u003E\u003C\/script\u003E"));</script> | |
<script src="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.6/bootstrap.min.js"></script> | |
<script>(window.jQuery && window.jQuery.fn && window.jQuery.fn.modal||document.write("\u003Cscript src=\u0022\/lib\/bootstrap\/dist\/js\/bootstrap.min.js\u0022\u003E\u003C\/script\u003E"));</script> | |
<script src="/js/site.min.js?v=4YtIaePNzexGu4QQcABZ3hmCTZ5PpZ6UoIpVvTVV2ww"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace WebMvcApplication.ViewModels | |
{ | |
public class SimpleViewModel | |
{ | |
// A message for our visitors | |
public string Message { get; set; } | |
// A hit counter, because it's 1999 apparently | |
public int HitCounter { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) | |
{ | |
if (env.IsDevelopment()) | |
{ | |
loggerFactory.AddConsole(Configuration.GetSection("Logging")); | |
loggerFactory.AddDebug(); | |
} | |
app.UseMvc(routes => | |
{ | |
routes.MapRoute( | |
name: "default", | |
template: "{controller=Home}/{action=Index}/{id?}"); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment