zoukankan      html  css  js  c++  java
  • 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", "1" }, { "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项目结构

  • 相关阅读:
    HTTPS协议详解
    HTTP协议详解
    网络传输协议 UDP & TCP 详解
    Socket(套接字)基础概念
    网络基础
    OSI 七层协议
    经典SQL题 1/25/50/100美分,多少种可能拼凑成2美元
    5.1一阶谓词逻辑
    4.4符号视角下的科学
    4.3领域语言与自然语言的比较
  • 原文地址:https://www.cnblogs.com/qiyebao/p/5130073.html
Copyright © 2011-2022 走看看