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", "" }, { "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 5 RC 1:UrlRouting 设置(不包含MVC6的UrlRouting设置)的更多相关文章
- ASP.NET 5 RC 2:UrlRouting 设置(不包含MVC6的UrlRouting设置)
0.Program.cs using System.IO; using Microsoft.AspNetCore.Hosting; namespace AspNetCoreUrlRoutingDemo ...
- ASP.NET Core 1:UrlRouting 设置(不包含MVC6的UrlRouting设置)
0.Program.cs using System.IO; using Microsoft.AspNetCore.Hosting; namespace WebApplication1 { public ...
- Asp.Net中应用Aspose.Cells输出报表到Excel 及样式设置
解决思路: 1.找个可用的Aspose.Cells(有钱还是买个正版吧,谁开发个东西也不容易): 2.在.Net方案中引用此Cells: 3.写个函数ToExcel(传递一个DataTable),可以 ...
- IIS 添加mime 支持 apk,exe,.woff,IIS MIME设置 ,Android apk下载的MIME 设置 苹果ISO .ipa下载mime 设置
原文:IIS 添加mime 支持 apk,exe,.woff,IIS MIME设置 ,Android apk下载的MIME 设置 苹果ISO .ipa下载mime 设置 站点--右键属性--http头 ...
- Web.config中的设置 forms 中的slidingExpiration的设置
在ASP.NET 网站中,使用 Forms Authentication时,一般的设置是如下的: <authentication mode="Forms"> <f ...
- 无线路由器的设置_不能通过wifi进行设置
昨天朋友的小区宽带续费完不能上网了,过去看了一下,无线路由器没有问题,但是宽带信号没过来,网线直接插在电脑上用拨号,发现根本没办法连接,提示网线已经被拔出,重新还原一下系统,也是不行.因为之前他的电脑 ...
- 实现ScrollView中包含ListView,动态设置ListView的高度
ScrollView 中包含 ListView 的问题 : ScrollView和ListView会冲突,会导致ListView显示不全 <?xml version="1.0" ...
- iOS “请在微信客户端打开链接” UIWebview加载H5页面携带session、cookie、User-Agent信息 设置cookie、清除cookie、设置User-Agent
公司新开的一个项目..内容基本上是加载H5页面显示..当时觉得挺简单的..后来发现自己掉坑里了..一些心理历程就不说了..说这个项目主要用到的知识点吧..也是自己踩得坑. 首先说说..这个项目上的内容 ...
- 设置checkbox选中,设置radio选中,根据值设置checkbox选中,checkbox勾选
设置checkbox选中,设置radio选中,根据值设置checkbox选中,checkbox勾选 >>>>>>>>>>>>&g ...
随机推荐
- 一次jdbc乱码解决
今天我做了一个小实验,从sqlserver 2010中将一张表转移到mysql中,使用的是基本的jdbc,前面复制的好好地,不知道怎么了,到了第三万行,突然出现了下面的异常 Incorrect str ...
- mysqli
CREATE TABLE `user` ( `id` ) NOT NULL, `name` ) NOT NULL, `password` ) NOT NULL ) ENGINE=InnoDB DEFA ...
- 迅为4412开发板Linux驱动教程——编写简单应用调用驱动
Linux驱动教程:http://pan.baidu.com/s/1c0hljUS 编写简单应用调用驱动--头文件 • 打印头文件 – include <stdio.h>调用打印函数pri ...
- ASP入门(二十三)- 数据库插入、更新和删除操作
我们这里介绍如何使用 Recordset 对象进行插入.更新和删除操作,顺便和 SQL 语句对比. 插入记录 AddNew 方法用于插入一条记录,首先打开一个记录集,并且这个记录具备可写特性,而后调用 ...
- Mockito各场景使用介绍
场景1:某三方接口所有方法都需要Mock 实现方式1-配置configrution bean 实现方式2-在application context中配置mock bean 场景2:某三方接口部分方法m ...
- JavaScript 作用域和闭包——另一个角度:扩展你对作用域和闭包的认识【翻译+整理】
原文地址 --这篇文章有点意思,可以扩展你对作用域和闭包的认识. 本文内容 背景 作用域 闭包 臭名昭著的循环问题 自调用函数(匿名函数) 其他 我认为,尝试向别人解释 JavaScript 作用域和 ...
- Javascript 的模块化编程及加载模块【转载+整理】
http://www.ruanyifeng.com/blog/2012/10/javascript_module.html 本文内容 引入 模块化 最初写法 对象写法 立即执行函数写法 放大模式 宽放 ...
- Android Device Monitor工具的使用
在最新的Android Studio3.x版本中,已经去掉了Android Device Monitor工具,但是不代表Android Device Monitor工具就不能用了,找到sdk的目录: ...
- 【树莓派】【转】树莓派3装Android 6.0,支持Wi-Fi和蓝牙
树莓派3装Android 6.0,支持Wi-Fi和蓝牙 相信对于许多树莓派初学者(包括我)来说,Android系统的确是一个不错的选择.但国内这方面资源稀缺,经本人FQ苦寻,找到了老外的树莓派Andr ...
- C++ 第十一课 标准c内存函数
calloc() 分配一个二维储存空间 free() 释放已分配空间 malloc() 分配空间 realloc() 改变已分配空间的大小 calloc 语法: #include <st ...