0、Program.cs

using System.IO;
using Microsoft.AspNetCore.Hosting; namespace WebApplication1
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build(); host.Run();
}
}
}

1、project.json

{
"userSecretsId": "aspnet-WebApplicationCore1-782de49b-8e7f-46be-82aa-0f48e1d370bc",
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0",
"type": "platform"
},
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.AspNetCore.Routing": "1.0.0",
"Microsoft.AspNetCore.Routing.Abstractions": "1.0.0",
"Microsoft.Extensions.Configuration.Abstractions": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Configuration.UserSecrets": "1.0.0",
"Microsoft.AspNetCore.Http.Abstractions": "1.0.0",
"Microsoft.AspNetCore.StaticFiles": "1.0.0"
}, "tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
}, "frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
}, "buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
}, "runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
}, "publishOptions": {
"include": [
"wwwroot",
"web.config"
]
}, "scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}

2、appsettings.json

{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-WebApplicationCore1-782de49b-8e7f-46be-82aa-0f48e1d370bc;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}

3、Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using WebApplication1.Route;
using Microsoft.Extensions.Configuration;
using WebApplication1.Extensions;
using Microsoft.Extensions.FileProviders;
using Microsoft.AspNetCore.StaticFiles; namespace WebApplication1
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
IConfigurationBuilder builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();//project.json -> userSecretsId
}
builder.AddEnvironmentVariables();
this.Configuration = builder.Build();
} public IConfigurationRoot Configuration { get; private set; } // 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();
services.AddDirectoryBrowser();//浏览所有文件
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//loggerFactory.AddConsole();
loggerFactory.AddConsole(this.Configuration.GetSection("Logging"));
loggerFactory.AddConsole(minLevel: LogLevel.Information);
loggerFactory.AddDebug();
//app.UseRequestIP(); if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
//app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/error");
} app.UseStaticFiles();//使用默认文件夹wwwroot var staticfile = new StaticFileOptions();
staticfile.FileProvider = new PhysicalFileProvider(@"C:\");//指定目录 这里指定C盘,也可以是其它目录
//你会发现有些文件打开会404,有些又可以打开。那是因为MIME 没有识别出来。
//我们可以手动设置这些 MIME ,也可以给这些未识别的设置一个默认值。
staticfile.ServeUnknownFileTypes = true;
staticfile.DefaultContentType = "application/x-msdownload"; //设置默认 MIME
var provider = new FileExtensionContentTypeProvider();
provider.Mappings.Add(".log", "text/plain");//手动设置对应MIME
staticfile.ContentTypeProvider = provider; app.UseStaticFiles(staticfile); var dir = new DirectoryBrowserOptions();
dir.FileProvider = new PhysicalFileProvider(@"C:\");
app.UseDirectoryBrowser(dir);
//对于前面的这么多设置,StaticFiles 提供了一种简便的写法。
app.UseFileServer(new FileServerOptions()
{
FileProvider = new PhysicalFileProvider(@"C:\"),
EnableDirectoryBrowsing = true
}); //UrlRouting
RouteBuilder routeBuilder = new RouteBuilder(app); //index http://localhost:5994/en
routeBuilder.DefaultHandler = new IndexPageRouteHandler(this.Configuration, "index");
routeBuilder.MapRoute("index_culture_", "{culture}/", new RouteValueDictionary { { "culture", "en" } }, new RouteValueDictionary { { "culture", @"\w{2}" } });
app.UseRouter(routeBuilder.Build()); //category http://localhost:5994/en/fashion/wwww-1111/2
routeBuilder.DefaultHandler = new CategoryPageRouteHandler(this.Configuration, "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());
}
}
}

4、IndexPageRouteHandler.cs

using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration; namespace WebApplication1.Route
{
public class IndexPageRouteHandler : 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.GetConnectionString("DefaultConnection");
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);
}
}
}

5、CategoryPageRouteHandler.cs

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using System;
using System.Diagnostics;
using System.Threading.Tasks; namespace WebApplication1.Route
{
public class CategoryPageRouteHandler : 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.GetConnectionString("DefaultConnection");
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);
}
}
}

6、F5启动调试,

浏览器输入网址:http://localhost:16924/

浏览器输入网址:http://localhost:16924/en/fashion/wwww-1111/2

ASP.NET Core 1:UrlRouting 设置(不包含MVC6的UrlRouting设置)的更多相关文章

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

    0.Program.cs using System.IO; using Microsoft.AspNetCore.Hosting; namespace AspNetCoreUrlRoutingDemo ...

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

    转自:http://habrahabr.ru/company/microsoft/blog/268037/?mobile=no 1.project.json { "version" ...

  3. ASP.NET CORE 2.0 发布到IIS,IIS如何设置环境变量来区分生产环境和测试环境

    0.前言 因为给前端的测试环境是windows,所以要设置windows上的环境变量,如果上Linux就没有这篇文章了,所以大家不要在意为什么core不放在linux上. 1.网上的解决方案 a 方式 ...

  4. (19)ASP.NET Core EF创建模型(包含属性和排除属性、主键、生成的值)

    1.什么是Fluent API? EF中内嵌的约定将POCO类映射到表.但是,有时您无法或不想遵守这些约定,需要将实体映射到约定指示外的其他对象,所以Fluent API和注解都是一种方法,这两种方法 ...

  5. ASP.Net Core中设置JSON中DateTime类型的格式化(解决时间返回T格式)

    最近项目有个新同事,每个API接口里返回的时间格式中都带T如:[2019-06-06T10:59:51.1860128+08:00],其实这个主要是ASP.Net Core自带时间格式列化时间格式设置 ...

  6. ASP.NET Core 中间件详解及项目实战

    前言 在上篇文章主要介绍了DotNetCore项目状况,本篇文章是我们在开发自己的项目中实际使用的,比较贴合实际应用,算是对中间件的一个深入使用了,不是简单的Hello World,如果你觉得本篇文章 ...

  7. ASP.NET Core 介绍

    原文:Introduction to ASP.NET Core 作者:Daniel Roth.Rick Anderson.Shaun Luttin 翻译:江振宇(Kerry Jiang) 校对:许登洋 ...

  8. 细说ASP.NET Core静态文件的缓存方式

    一.前言 我们在优化Web服务的时候,对于静态的资源文件,通常都是通过客户端缓存.服务器缓存.CDN缓存,这三种方式来缓解客户端对于Web服务器的连接请求压力的. 本文指在这三个方面,在ASP.NET ...

  9. [转]ASP.NET Core 中间件详解及项目实战

    本文转自:http://www.cnblogs.com/savorboard/p/5586229.html 前言 在上篇文章主要介绍了DotNetCore项目状况,本篇文章是我们在开发自己的项目中实际 ...

随机推荐

  1. 从0开始学Swift笔记整理(一)

    Swift 是一种适用于 iOS 和 OS X 应用的全新编程语言,它建立在最好的 C 和 Objective-C 语言之上,并且没有 C 语言的兼容性限制.Swift 采用安全的编程模式,增加了现代 ...

  2. 探究Repository模式的两种写法与疑惑

    现如今DDD越来越流行,园子里漫天都是介绍关于它的文章.说到DDD就不能不提Repository模式了,有的地方也叫它仓储模式. 很多时候我们对Repository都还停留在Copy然后使用的阶段, ...

  3. Linux下oracle11gR2系统安装到数据库建立配置及最后oracle的dmp文件导入一站式操作记录

    简介 之前也在linux下安装过oralce,可每次都是迷迷糊糊的,因为大脑一片空白,网上随便看见一个文档就直接复制,最后搞了乱七八糟,虽然装上了,却乱得很,现在记录下来,希望能给其他网上朋友遇到问题 ...

  4. Asp.NET MVC 拍卖网站,拆解【2】 Asp.NET MVC章回,第(1)节

    时间和篇幅所限,MVC不会介绍基本的建站过程,请参照博客园技术专题文章传送门  英语足够好的请直接去微asp.net 官网 传送门(强烈推荐,尤其是想使用最新技术的时候更应该直接去官网),本文主要介绍 ...

  5. 跟我一起学WCF(4)——第一个WCF程序

    一.引言 前面几篇文章分享了.NET 平台下其他几种分布式技术,然而前面几种分布式技术专注于某一特定的领域,并且具有不同编程接口,这使得开发人员需要掌握多个API的使用.基于这样的原因,微软在.NET ...

  6. ZooKeeper 3.5.0 分布式配置问题

    ZooKeeper 3.5.0 分布式配置好后,执行./zkServer.sh start 命令启动,报如下错误: 2015-07-02 21:06:01,671 [myid:] - INFO [ma ...

  7. 一致性Hash算法在Redis分布式中的使用

    由于redis是单点,但是项目中不可避免的会使用多台Redis缓存服务器,那么怎么把缓存的Key均匀的映射到多台Redis服务器上,且随着缓存服务器的增加或减少时做到最小化的减少缓存Key的命中率呢? ...

  8. Java Web指导方向

    第一阶段 第二阶段 第三阶段 第四阶段 第五阶段 第六阶段 第七阶段 第一阶段:Java程序员 技术名称 内                 容 说明 Java语法基础 基本语法.数组.类.继承.多态 ...

  9. AT&T ASSEMBLY FOR LINUX AND MAC (SYS_FORK)

    Fork() in C: (sys_fork.c) #include <stdio.h> #include <stdlib.h> #include <unistd.h&g ...

  10. paip.网页右键复制菜单限制解除解决方案

    paip.网页右键复制菜单限制解除解决方案 作者Attilax  艾龙,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.csdn.net ...