转自:http://habrahabr.ru/company/microsoft/blog/268037/?mobile=no
1、project.json
1 { 2 "version": "1.0.0-*", 3 "compilationOptions": { 4 "emitEntryPoint": true 5 }, 6 7 "dependencies": { 8 "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final", 9 "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final", 10 "Microsoft.AspNet.Routing": "1.0.0-rc1-final", 11 "Microsoft.Framework.DependencyInjection": "1.0.0-beta8" 12 }, 13 14 "commands": { 15 "web": "Microsoft.AspNet.Server.Kestrel" 16 }, 17 18 "frameworks": { 19 "dnx451": { }, 20 "dnxcore50": { } 21 }, 22 23 "exclude": [ 24 "wwwroot", 25 "node_modules" 26 ], 27 "publishExclude": [ 28 "**.user", 29 "**.vspscc" 30 ] 31 }
project.json
2、Startup.cs
1 using Microsoft.AspNet.Builder; 2 using Microsoft.AspNet.Hosting; 3 using Microsoft.AspNet.Routing; 4 using Microsoft.Extensions.DependencyInjection; 5 using WebApplication1.PageRoute; 6 7 namespace WebApplication1 8 { 9 public class Startup 10 { 11 // This method gets called by the runtime. Use this method to add services to the container. 12 // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 13 public void ConfigureServices(IServiceCollection services) 14 { 15 services.AddRouting(); 16 } 17 18 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 19 public void Configure(IApplicationBuilder app) 20 { 21 app.UseIISPlatformHandler(); 22 23 RouteBuilder routeBuilder = new RouteBuilder(); 24 routeBuilder.ServiceProvider = app.ApplicationServices; 25 26 //index 27 routeBuilder.DefaultHandler = new IndexPageRouteHandler("index"); 28 routeBuilder.MapRoute("index_culture_", "{culture}/", new RouteValueDictionary { { "culture", "en" } }, new RouteValueDictionary { { "culture", @"\w{2}" } }); 29 app.UseRouter(routeBuilder.Build()); 30 31 //category 32 routeBuilder.DefaultHandler = new CategoryPageRouteHandler("category"); 33 routeBuilder.MapRoute("category_", "{culture}/fashion/{leimu}/{pageindex}/", new RouteValueDictionary { { "pageindex", "1" }, { "culture", "en" } }, new RouteValueDictionary { { "leimu", "([\\w|-]+)(\\d+)" }, { "pageindex", "\\d+" }, { "culture", @"\w{2}" } }); 34 app.UseRouter(routeBuilder.Build()); 35 } 36 37 // Entry point for the application. 38 public static void Main(string[] args) => WebApplication.Run<Startup>(args); 39 } 40 }
Startup.cs
3、IndexPageRouteHandler.cs
1 using System; 2 using System.Threading.Tasks; 3 using Microsoft.AspNet.Routing; 4 using Microsoft.AspNet.Http; 5 6 namespace WebApplication1.PageRoute 7 { 8 public class IndexPageRouteHandler : Microsoft.AspNet.Routing.IRouter 9 { 10 private string _name = null; 11 12 public IndexPageRouteHandler(string name) 13 { 14 this._name = name; 15 } 16 17 public VirtualPathData GetVirtualPath(VirtualPathContext context) 18 { 19 throw new NotImplementedException(); 20 } 21 22 public async Task RouteAsync(RouteContext context) 23 { 24 var routeValues = string.Join("", context.RouteData.Values); 25 var message = String.Format("{0} Values={1} ", this._name, routeValues); 26 await context.HttpContext.Response.WriteAsync(message); 27 context.IsHandled = true; 28 } 29 } 30 }
IndexPageRouteHandler.cs
4、CategoryPageRouteHandler.cs
1 using Microsoft.AspNet.Http; 2 using Microsoft.AspNet.Routing; 3 using System; 4 using System.Threading.Tasks; 5 6 namespace WebApplication1.PageRoute 7 { 8 public class CategoryPageRouteHandler : Microsoft.AspNet.Routing.IRouter 9 { 10 private string _name = null; 11 12 public CategoryPageRouteHandler(string name) 13 { 14 this._name = name; 15 } 16 17 public VirtualPathData GetVirtualPath(VirtualPathContext context) 18 { 19 throw new NotImplementedException(); 20 } 21 22 public async Task RouteAsync(RouteContext context) 23 { 24 var routeValues = string.Join("", context.RouteData.Values); 25 var message = String.Format("{0} Values={1} ", this._name, routeValues); 26 await context.HttpContext.Response.WriteAsync(message); 27 context.IsHandled = true; 28 } 29 } 30 }
CategoryPageRouteHandler.cs
5、F5启动调试,
浏览器输入网址:http://localhost:16924/
浏览器输入网址:http://localhost:16924/en/fashion/wwww-1111/2
6、VS2015项目结构
时间: 2024-10-06 04:31:57