ASP.NET 5 UrlRouting 设置(不包含MVC6的UrlRouting设置)

转自: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

ASP.NET 5 UrlRouting 设置(不包含MVC6的UrlRouting设置)的相关文章

ASP.NET 5 RC 2:UrlRouting 设置(不包含MVC6的UrlRouting设置)

0.Program.cs using System.IO; using Microsoft.AspNetCore.Hosting; namespace AspNetCoreUrlRoutingDemoRC2 { public class Program { public static void Main(string[] args) { var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurre

ASP.NET Session State(会话状态)SQLServer模式和设置

Thursday, December 5, 2013 ASP.NET保存session state(会话状态)有三种模式:In-Process, State Server, SQL Server.SQL Server模式的优点:    即使web application重启,状态数据也会被保存    多个web服务器可以共享状态数据    节省内存设置:    在web.config中,使用mode="SQLServer",例如:    <configuration>   

android中scrollview包含listView对高度设置

当scrollview中包含listView时,滑动的时候会出现问题..最常见的问题是listview滑动不了...问题原因是在加载listview时它的高度应该按照adapter所绑定的数据来确定,而不是fill_parent或者是wrap_content... 项目中用到了就把它抽取出来做个记录 import android.view.View; import android.view.ViewGroup; import android.widget.ListAdapter; import

centos 6.4 修改ssh默认端口22为其他端口详尽安全教程(包含SELinux,iptables设置)

该教程是目前最安全的教程,如果你是新手请严格按照教程的步骤进行,如果是有一定基础的人可以选择性跳过某些段落 修改端口配置 先运行 vim /etc/ssh/sshd_config 找到#Port 22 这行然后去掉前面的注释,然后再下面加一行 Port 1234 Port 22 Port 1234 很多教程是直接修改22为其他端口,最好不要这样做,万一修改的端口不能用,你就完全登陆不上了,所以先留一条后路 修改防火墙配置 修改iptables(linux 的防火墙)文件: vim /etc/sy

微软ASP.NET 电商网站开发实战 MVC6 +HTML5 +WCF+WebAPI+NoSQL+mongoDB+Redis+Core视频 代码 面试题

<微软ASP.NET 电商网站开发实战 MVC6 +HTML5 +WCF+WebAPI+NoSQL+mongoDB+Redis+Core 视频 代码 面试题 >下载网盘:https://yunpan.cn/cP7SNIjgJYYjA  访问密码 7fc6 微软特邀讲师 徐雷FrankXuLei 2016 授课 更新:.NET Core 1.0高并发框架+面试题更新:高性能缓存 Redis.NoSQL面试题 安装,增删改查 RedisHelper帮助类 购物车 会话服务器更新:REST WebA

[Flex] 组件Tree系列 —— 运用LabelFunction hasChildren getChildren设置Tree包含节点个数

mxml: 1 <?xml version="1.0" encoding="utf-8"?> 2 3 <!--功能描述:运用LabelFunction hasChildren getChildren设置Tree包含节点个数--> 4 5 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 6 xmlns:s="library://ns.adobe.

[ASP.NET MVC] URL网址路由包含&ldquo;/&rdquo;斜线

ASP.NET MVC 网址包含"/"斜线 笔者碰到一个状况是,URL带入的参数是可能有"/"斜线的(使用者输入)像是以下 1.运行正常 http://localhost:45405/Home/Product?name=HTC/M8/手机套 2.运行正常 http://localhost:45405/Home/Product?name='抗涨'白人牙膏_特大号 到目前为止都还没问题,但我们为了让URL好看以及优化SEO,我们会做URL Rewriting像是以下 1

ASP网站建立不成功是因为 web服务扩展没有设置好

ASP网站建立不成功是因为 web服务扩展没有设置好 原文地址:https://blog.51cto.com/215363/2446865

里面的div没有设置宽高,用padding设置的50px来撑开宽高,这个时候就需要用position:absolute或者relative来让文本去掉继承的外层的整宽度

1. <!DOCTYPE html><html><head><style>*{margin:0px;padding:0px;}#div1{position: relative;height: 200px;width: 200px;margin: 100px;padding:10px;border: 1px solid black;} #div2{padding:50px;position: absolute;border: 1px solid black;b