/// <summary>
    /// Add rules from an Apache mod_rewrite file
    /// </summary>
    /// <param name="options">The <see cref="RewriteOptions"/></param>
    /// <param name="fileProvider">The <see cref="IFileProvider"/> </param>
    /// <param name="filePath">The path to the file containing mod_rewrite rules.</param>
    public static RewriteOptions AddApacheModRewrite(this RewriteOptions options, IFileProvider fileProvider, string filePath)
    {
        if (options == null)
        {
            throw new ArgumentNullException(nameof(options));
        }

        if (fileProvider == null)
        {
            throw new ArgumentNullException(nameof(fileProvider));
        }

        var fileInfo = fileProvider.GetFileInfo(filePath);

        using (var stream = fileInfo.CreateReadStream())
        {
            return(options.AddApacheModRewrite(new StreamReader(stream)));
        }
    }
示例#2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            #region URL 跳转

            var options = new RewriteOptions();
            options.AddRedirect("xcode/(.*)", "blog/$1")
            // 可以链式配置
            // .AddRedirect()
            // 可以重写
            .AddRewrite(@"test1/(\d+)", "test2/$1", true)
            // 配置 https
            .AddRedirectToHttps()
            ;

            // 根据配置文件 Apache
            using (StreamReader streamReader = File.OpenText(""))
            {
                options.AddApacheModRewrite(reader: streamReader);
            }

            // 根据配置文件 IIS
            using (StreamReader streamReader = File.OpenText(""))
            {
                options.AddIISUrlRewrite(reader: streamReader);
            }

            // 多种自定义添加方式
            options.Add(RedirectHelper.RedirectUrl);
            options.Add(new CustomerRule());


            app.UseRewriter(options);

            #endregion


            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context => { await context.Response.WriteAsync("Hello World!"); });
            });
        }
示例#3
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            //重写中间件演示
            //var option = new RewriteOptions();
            //option.AddRedirect(@"xcode/(\d+)", "blog/$1")
            //    .AddRewrite(@"baidu/([a-z]{2,3})", "blog/$1", true)
            //    .AddRedirectToHttps();

            //app.UseRewriter(option);

            #region 使用配置文件设置重定向和重写规则

            var option = new RewriteOptions();
            //使用Apache配置文件
            using (StreamReader reader = File.OpenText("ApacheConfig.txt"))
            {
                option.AddApacheModRewrite(reader);
            }
            //使用IIS配置文件
            using (StreamReader reader = File.OpenText("IISUrlRewrite.xml"))
            {
                option.AddIISUrlRewrite(reader);
            }
            //使用委托
            option.Add(RewriteHelper.RedirectUrl);
            option.Add(RewriteHelper.RewriteUrl);
            //使用接口
            //option.Add(new CustomRule());

            app.UseRewriter(option);

            #endregion

            app.Run(async(context) =>
            {
                var url = context.Request.Protocol + context.Request.Host + context.Request.Path;
                await context.Response.WriteAsync("Hello World! " + url);
            });
        }