Created
January 6, 2015 20:04
-
-
Save peterblazejewicz/b5915283a5ff23d468a6 to your computer and use it in GitHub Desktop.
vNext ViewComponent boilerplate built upon aspnet-generator's mvc sample (note: check paths in commented code)
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
@* Views/Home/Shared/Components/PageHeader/ *@ | |
@using MvcSample.Web | |
@model PageHeader | |
<div class="page-header"> | |
<h1>Hello, @Model.FullName <small>Last visit: @Model.LastVisit</small></h1> | |
</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
@* Views/Home/Index.cshtml *@ | |
@using MvcSample.Web | |
@{ | |
Layout = "/Views/Shared/_Layout.cshtml"; | |
ViewBag.Title = "Home Page"; | |
string helloClass = null; | |
} | |
<div class="jumbotron"> | |
<h1>ASP.NET</h1> | |
<p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p> | |
<p><a href="http://asp.net" class="btn btn-primary btn-large" role="button">Learn more »</a></p> | |
</div> | |
<div class="row"> | |
<div class="col-lg-12"> | |
@await Component.InvokeAsync("PageHeader") | |
</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 System.Threading.Tasks; | |
using System; | |
namespace MvcSample.Web | |
{ | |
public interface IPageHeaderManager | |
{ | |
Task<PageHeader> GetPageHeaderAsync(); | |
} | |
public class PageHeaderManager: IPageHeaderManager | |
{ | |
private PageHeader pageHeader = new PageHeader | |
{ | |
FullName = "Peter Blazejewicz", | |
LastVisit = "Yesterday" | |
}; | |
public Task<PageHeader> GetPageHeaderAsync() | |
{ | |
return Task.FromResult<PageHeader>(pageHeader); | |
} | |
} | |
public class PageHeader | |
{ | |
public string FullName { get; set; } | |
public string LastVisit { 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
// Components/PageHeaderViewComponent.cs | |
using System.Threading.Tasks; | |
using System; | |
using Microsoft.AspNet.Mvc; | |
namespace MvcSample.Web.Components | |
{ | |
public class PageHeaderViewComponent: ViewComponent | |
{ | |
private readonly IPageHeaderManager pageHeaderManager; | |
public PageHeaderViewComponent(IPageHeaderManager pageHeaderManager) | |
{ | |
if(pageHeaderManager == null) | |
{ | |
throw new ArgumentNullException("IPageHeaderManager"); | |
} | |
this.pageHeaderManager = pageHeaderManager; | |
} | |
public async Task<IViewComponentResult> InvokeAsync() | |
{ | |
var pageHeader = await pageHeaderManager.GetPageHeaderAsync(); | |
return View(pageHeader); | |
} | |
} | |
} |
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.AspNet.Builder; | |
using Microsoft.AspNet.Routing; | |
using Microsoft.Framework.DependencyInjection; | |
namespace MvcSample.Web | |
{ | |
public class Startup | |
{ | |
public void Configure(IApplicationBuilder app) | |
{ | |
app.UseStaticFiles(); | |
app.UseServices(services => | |
{ | |
services.AddMvc(); | |
services.AddScoped<IPageHeaderManager, PageHeaderManager>(); | |
}); | |
// Add MVC to the request pipeline | |
app.UseMvc(routes => | |
{ | |
routes.MapRoute( | |
name: "default", | |
template: "{controller}/{action}/{id?}", | |
defaults: new { controller = "Home", action = "Index" }); | |
routes.MapRoute( | |
name: "api", | |
template: "{controller}/{id?}"); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment