ASP.NET 5 RC 1:UrlRouting 设置(不包含MVC6的UrlRouting设置)
转自:http://habrahabr.ru/company/microsoft/blog/268037/?mobile=no
1、project.json
{ "version": "1.0.0-*", "compilationOptions": { "emitEntryPoint": true }, "dependencies": { "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final", "Microsoft.AspNet.Routing": "1.0.0-rc1-final", "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final", "Microsoft.Extensions.Configuration": "1.0.0-rc1-final" }, "commands": { "web": "Microsoft.AspNet.Server.Kestrel" }, "frameworks": { "dnx451": { }, "dnxcore50": { } }, "exclude": [ "wwwroot", "node_modules" ], "publishExclude": [ "**.user", "**.vspscc" ] }
2、appsettings.json
{ "Data": { "DefaultConnection": { "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;" } } }
3、Startup.cs
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Hosting; using Microsoft.Extensions.DependencyInjection; using AspNetCoreUrlRoutingDemo.PageRoute; using Microsoft.AspNet.Routing; using Microsoft.Extensions.Configuration; namespace AspNetCoreUrlRoutingDemo { /// <summary> /// http://www.admin10000.com/document/7071.html /// </summary> public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddRouting(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app) { app.UseIISPlatformHandler(); IConfigurationBuilder builder = new ConfigurationBuilder(); builder.AddJsonFile("appsettings.json"); IConfigurationRoot root = builder.Build(); RouteBuilder routeBuilder = new RouteBuilder(); routeBuilder.ServiceProvider = app.ApplicationServices; //index routeBuilder.DefaultHandler = new IndexPageRouteHandler(root, "index"); routeBuilder.MapRoute("index_culture_", "{culture}/", new RouteValueDictionary { { "culture", "en" } }, new RouteValueDictionary { { "culture", @"\w{2}" } }); app.UseRouter(routeBuilder.Build()); //category routeBuilder.DefaultHandler = new CategoryPageRouteHandler(root, "category"); routeBuilder.MapRoute("category_", "{culture}/fashion/{leimu}/{pageindex}/", new RouteValueDictionary { { "pageindex", "1" }, { "culture", "en" } }, new RouteValueDictionary { { "leimu", "([\\w|-]+)(\\d+)" }, { "pageindex", "\\d+" }, { "culture", @"\w{2}" } }); app.UseRouter(routeBuilder.Build()); } // Entry point for the application. public static void Main(string[] args) => WebApplication.Run<Startup>(args); } }
4、IndexPageRouteHandler.cs
using System; using System.Threading.Tasks; using Microsoft.AspNet.Http; using Microsoft.AspNet.Routing; using Microsoft.Extensions.Configuration; using System.Diagnostics; namespace AspNetCoreUrlRoutingDemo.PageRoute { public class IndexPageRouteHandler : Microsoft.AspNet.Routing.IRouter { private string _name = null; private readonly IConfigurationRoot _configurationRoot; public IndexPageRouteHandler(IConfigurationRoot configurationRoot, string name) { this._configurationRoot = configurationRoot; this._name = name; } public VirtualPathData GetVirtualPath(VirtualPathContext context) { throw new NotImplementedException(); } public async Task RouteAsync(RouteContext context) { if (this._configurationRoot != null) { string connectionString = this._configurationRoot.Get<string>("Data:DefaultConnection:ConnectionString"); Debug.WriteLine(connectionString); } var routeValues = string.Join("", context.RouteData.Values); var message = String.Format("{0} Values={1} ", this._name, routeValues); await context.HttpContext.Response.WriteAsync(message); context.IsHandled = true; } } }
5、CategoryPageRouteHandler.cs
using System; using System.Threading.Tasks; using Microsoft.AspNet.Http; using Microsoft.AspNet.Routing; using Microsoft.Extensions.Configuration; using System.Diagnostics; namespace AspNetCoreUrlRoutingDemo.PageRoute { public class CategoryPageRouteHandler : Microsoft.AspNet.Routing.IRouter { private string _name = null; private readonly IConfigurationRoot _configurationRoot; public CategoryPageRouteHandler(IConfigurationRoot configurationRoot, string name) { this._configurationRoot = configurationRoot; this._name = name; } public VirtualPathData GetVirtualPath(VirtualPathContext context) { throw new NotImplementedException(); } public async Task RouteAsync(RouteContext context) { if (this._configurationRoot != null) { string connectionString = this._configurationRoot.Get<string>("Data:DefaultConnection:ConnectionString"); Debug.WriteLine(connectionString); } var routeValues = string.Join("", context.RouteData.Values); var message = String.Format("{0} Values={1} ", this._name, routeValues); await context.HttpContext.Response.WriteAsync(message); context.IsHandled = true; } } }
6、F5启动调试,
浏览器输入网址:http://localhost:16924/
浏览器输入网址:http://localhost:16924/en/fashion/wwww-1111/2
6、VS2015项目结构
分类:
ASP.NET Core
标签:
asp.net core
, url routing
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现