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 ...
随机推荐
- javascript奇技淫巧之位运算符
奇技淫巧:指过于奇巧而无益还让人着迷的技艺与制品. And(与) & Or(或) | Exclusive Or(异或) 或者称 Xor ^ Not(非) ~ 位运算符,我们在日常js开发中其实 ...
- ArcGIS10.3新体验
自2012年ESRI更新10.2以后,终于在2014年12月8日,官方推出了10.3版本,前几天忙于抢票,今天终于可以在虚拟机中体验一把. 由于使用的是预览版,所有安装包只有800多M,包括桌面核心程 ...
- docker toolbox在win7下的安装
1.下载安装docker toolbox docker toolbox的下载地址: http://mirrors.aliyun.com/docker-toolbox/windows/docker-to ...
- Windows上Boost的编译步骤
一.FQ下载Boost最新版本 官网:http://www.boost.org/ 假设解压到:D:\Applicaton\DevTools\boost\boost_1_65_1 二.使用VS编译器 c ...
- Ubuntu/Centos 系统上安装与配置Apache
一.在线安装: Ubuntu:sudo apt-get install apache2 Centos: sudo yum install apache2 二.安装后的位置: 1.服务地址:/etc/i ...
- Java通过ScriptEngine 执行js脚本案例
public static void main(String[] args) throws ScriptException, FileNotFoundException, NoSuchMethodEx ...
- POSTGRESQL 自动登录
以前习惯使用MYSQL命令行登录,但是到POSTGRESQL不能实现,下面总结一下方法: 1.填写需要链接的postgresql语句,一般放在~/.bash_profile,例如: alias log ...
- js 树结构数据遍历条件判断
代码: /** * 树结构数据条件过滤 * js 指定删除数组(树结构数据) */ function filter (data, id) { var newData = data.filter(x = ...
- 开源 JSON 库解析性能对比( Jackson / Json.simple / Gson )
Json 已成为当前服务器与 web 应用之间数据传输的公认标准. 微服务及分布式架构经常会使用 Json 来传输此类文件,因为这已经是 webAPI 的事实标准. 不过正如许多我们习以为常的事情一样 ...
- Spring学习笔记二:注入方式
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6774608.html 我们说,IOC的实现方式是依赖注入,也就是把被依赖对象赋值到依赖对象的成员属性.怎么做 ...