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,32 +7,27 @@ namespace MvcSample.Web
{ {
public class Startup public class Startup
{ {
public void Configuration(IBuilder builder) public void Configuration(IBuilder app)
{ {
var services = new ServiceCollection(); app.UseContainer(services =>
services.AddMvc();
services.AddSingleton<PassThroughAttribute, PassThroughAttribute>();
var serviceProvider = services.BuildServiceProvider(builder.ServiceProvider);
var routes = new RouteCollection()
{ {
DefaultHandler = new MvcRouteHandler(), services.AddMvc();
}; services.AddSingleton<PassThroughAttribute, PassThroughAttribute>();
});
// TODO: Add support for route constraints, so we can potentially constrain by existing routes app.UseMvc(routes =>
routes.MapRoute("{area}/{controller}/{action}"); {
// TODO: Add support for route constraints, so we can potentially constrain by existing routes
routes.MapRoute("{area}/{controller}/{action}");
routes.MapRoute( routes.MapRoute(
"{controller}/{action}", "{controller}/{action}",
new { controller = "Home", action = "Index" }); new { controller = "Home", action = "Index" });
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>