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.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-rc2-3002702",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Routing": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Routing.Abstractions": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Http.Extensions": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.Binder": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.Abstractions": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Identity": "1.0.0-rc2-final",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc2-final"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    }
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "dnxcore50",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "gcServer": 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.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using AspNetCoreUrlRoutingDemoRC2.PageRoute;

namespace AspNetCoreUrlRoutingDemoRC2
{
    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; }

        // 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();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/error");
            }

            app.UseStaticFiles();
            //app.UseIdentity();

            RouteBuilder routeBuilder = new RouteBuilder(app);

            //index
            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
            routeBuilder.DefaultHandler = new CategoryPageRouteHandler(this.Configuration, "category");
            routeBuilder.MapRoute("category_", "{culture}/fashion/{leimu}/{pageindex}/", new RouteValueDictionary { { "pageindex", "1" }, { "culture", "en" } }, new RouteValueDictionary { { "leimu", "([\\w|-]+)(\\d+)" }, { "pageindex", "\\d+" }, { "culture", @"\w{2}" } });

            app.UseRouter(routeBuilder.Build());

        }
    }
}

4、IndexPageRouteHandler.cs

using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Http;
using System.Diagnostics;

namespace AspNetCoreUrlRoutingDemoRC2.PageRoute
{
    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 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);
        }

        public VirtualPathData GetVirtualPath(VirtualPathContext context)
        {
            throw new NotImplementedException();
        }
    }
}

5、CategoryPageRouteHandler.cs

using Microsoft.AspNetCore.Routing;
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using System.Diagnostics;

namespace AspNetCoreUrlRoutingDemoRC2.PageRoute
{
    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 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);
        }

        public VirtualPathData GetVirtualPath(VirtualPathContext context)
        {
            throw new NotImplementedException();
        }
    }
}

6、F5启动调试,

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

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

6、VS2015项目结构

时间: 2024-11-05 23:24:30

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

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.As

Web.config中的设置 forms 中的slidingExpiration的设置

在ASP.NET 网站中,使用 Forms Authentication时,一般的设置是如下的: <authentication mode="Forms"> <forms name="yourAuthCookie" loginUrl="/Login" protection="All" path="/" timeout="20" requireSSL="true

里面的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

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

UGUI 锚点设置为四方扩充模式然后设置局部坐标为0将出现什么问题

UGUI 锚点设置为四方扩充模式然后设置局部坐标为0将出现什么问题? 情形:按钮A挂在主画布上.四方扩充模式.A的中心和画面中心不重合. 这时候用代码设置A.localPosition = new Vector3(0,0,0)会改变A的位置吗? 答案:会. 原因:A.localPosition = new Vector3(0,0,0)是将A的中心重合到画布中心了.

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

linux 虚拟机centos64位_6.5+VM10 主机是固定IP局域网设置代理上网,虚机设置固定ip 图文详细步骤

一种: 虚机是Desktop 安装 1.虚拟机—设置—网络适配器子选项—选择“桥接模式” 2.在虚拟机中选择系统(System)—首选项(Preferences)—网络连接(Network Connections) 3.有则点击 SYstem eth0 进行编辑,没有就选择添加再进行编辑,选择IPv4设置(IPv4 Settings)  —添加(Add) 双击进行添加 (子网掩码,网关,DNS都设置成和主机一样,ip地址可设置成同一个网段的),设置完成后,点击应用(Apply...) 4.设置代

[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.

关于table边框,设置了border-collapse:collapse之后,设置border-radius没效果

做项目遇到边框需要设置圆角,然后发现在设置了border-collapse:collapse之后,border-radius:10px不起作用了,发现这个是css本身的问题,两者不能混在一起使用. 代码: <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title></title> <style> body