Added UseMvc extension method via IBuilder extensions.

This commit is contained in:
David Fowler 2014-04-17 19:55:23 -07:00
parent b8eb16d98d
commit 73e1386724
3 changed files with 41 additions and 23 deletions

View File

@ -1,6 +1,4 @@
using Microsoft.AspNet.Abstractions; using Microsoft.AspNet.Abstractions;
using Microsoft.AspNet.DependencyInjection;
using Microsoft.AspNet.DependencyInjection.Fallback;
using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.RequestContainer; using Microsoft.AspNet.RequestContainer;
using Microsoft.AspNet.Routing; using Microsoft.AspNet.Routing;
@ -9,19 +7,16 @@ namespace MvcSample.Web
{ {
public class Startup public class Startup
{ {
public void Configuration(IBuilder builder) public void Configuration(IBuilder app)
{
app.UseContainer(services =>
{ {
var services = new ServiceCollection();
services.AddMvc(); services.AddMvc();
services.AddSingleton<PassThroughAttribute, PassThroughAttribute>(); services.AddSingleton<PassThroughAttribute, PassThroughAttribute>();
});
var serviceProvider = services.BuildServiceProvider(builder.ServiceProvider); app.UseMvc(routes =>
var routes = new RouteCollection()
{ {
DefaultHandler = new MvcRouteHandler(),
};
// TODO: Add support for route constraints, so we can potentially constrain by existing routes // TODO: Add support for route constraints, so we can potentially constrain by existing routes
routes.MapRoute("{area}/{controller}/{action}"); routes.MapRoute("{area}/{controller}/{action}");
@ -32,9 +27,7 @@ namespace MvcSample.Web
routes.MapRoute( routes.MapRoute(
"{controller}", "{controller}",
new { controller = "Home" }); new { controller = "Home" });
});
builder.UseContainer(serviceProvider);
builder.UseRouter(routes);
} }
} }
} }

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNet.Abstractions;
using Microsoft.AspNet.Routing;
namespace Microsoft.AspNet.Mvc
{
public static class BuilderExtensions
{
public static IBuilder UseMvc(this IBuilder app, Action<IRouteCollection> configureRoutes)
{
var routes = new RouteCollection
{
DefaultHandler = new MvcRouteHandler()
};
// REVIEW: Consider adding UseMvc() that automagically adds the default MVC route
configureRoutes(routes);
return app.UseRouter(routes);
}
}
}

View File

@ -20,6 +20,7 @@
<Content Include="Project.json" /> <Content Include="Project.json" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="BuilderExtensions.cs" />
<Compile Include="ServiceCollectionExtensions.cs" /> <Compile Include="ServiceCollectionExtensions.cs" />
<Compile Include="MvcServices.cs" /> <Compile Include="MvcServices.cs" />
</ItemGroup> </ItemGroup>