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

最新文章

  1. cron(CronTrigger)表达式用法
  2. iOS-设置UIPageControl 显示图片
  3. ios录音
  4. iOS开发--隐藏(去除)导航栏底部横线
  5. tyvj1018 - 阶乘统计 ——暴力
  6. Struts2笔记——Action校验器
  7. 谈网页游戏外挂之用python模拟游戏(热血三国2)登陆
  8. 【转】 SqlServer性能检测和优化工具使用详细
  9. POJ 2553 The Bottom of a Graph TarJan算法题解
  10. 三十项调整助力 Ubuntu 13.04 更上一层楼
  11. Pencil OJ 02 安装
  12. 用php和imagemagick来处理图片文件的上传和缩放处理
  13. Cocos2dx 学习笔记整理----在项目中使用图片(二)
  14. (中等) POJ 2948 Martian Mining,DP。
  15. HDU2089 暴力打表
  16. 原生js怎样获取后台端口数据
  17. Ubuntu 下命令安装 Java
  18. jQuery与JS中的map()方法使用
  19. php中静态方法的使用
  20. 用大白话谈谈XSS与CSRF

热门文章

  1. Winform 中的KeyDown
  2. Makecert.exe(证书创建工具)
  3. innodb_ft_max_token_size取值范围
  4. C++ 升级到 Vs2013后编译设置
  5. MVC中用Jpaginate分页 So easy!(兼容ie家族)
  6. 04-Vue入门系列之Vue事件处理
  7. Gradle的属性设置大全
  8. servlet servlet基本概念和helloservlet实例
  9. (笔记)VC6插件安装(VC6LineNumberAddin)
  10. 将外卖O2O广告一棍子打成竞价排名,秤把平了吗?