{"id":659,"date":"2019-09-26T13:43:16","date_gmt":"2019-09-26T04:43:16","guid":{"rendered":"http:\/\/csharp.ihavenomoney.co.kr\/?p=659"},"modified":"2019-09-26T14:06:15","modified_gmt":"2019-09-26T05:06:15","slug":"rewrite-2","status":"publish","type":"post","link":"https:\/\/csharp.ihavenomoney.co.kr\/?p=659","title":{"rendered":"rewrite-2"},"content":{"rendered":"\n<pre class=\"lang:c# decode:true \" >using Microsoft.AspNetCore.Hosting; \/\/IWebHostBuilder\r\nusing Microsoft.AspNetCore.Builder; \/\/IApplicationBuilder\r\nusing Microsoft.AspNetCore.Http; \/\/WriteAsync\r\nusing Microsoft.Extensions.DependencyInjection;\r\nusing Microsoft.Extensions.Logging;\r\nusing Microsoft.AspNetCore;\r\nusing Microsoft.AspNetCore.Routing;\r\nusing Microsoft.AspNetCore.Rewrite; \/\/UseRewriter\r\nusing System.Net;\r\n\r\nnamespace WebApplication1\r\n{\r\n    public class Startup\r\n    {\r\n        public Startup(IHostingEnvironment env, ILoggerFactory logger)\r\n        {\r\n        }\r\n\r\n        public void ConfigureServices(IServiceCollection services)\r\n        {\r\n            \/\/This is the only service available at ConfigureServices\r\n            services.AddRouting();\r\n        }\r\n\r\n        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory logger)\r\n        {\r\n            var options = new RewriteOptions()\r\n               .AddRedirect(\"([\/_0-9a-z-]+)+(.*)$\", \"\/?path=$1&amp;ext=$2\"); \/\/redirect any path that ends with .html \r\n              \/\/.AddRewrite(\"([\/_0-9a-z-]+)+(.*)$\", \"\/?path=$1&ext=$2\", skipRemainingRules: false); \/\/redirect any path that ends with .html \r\n             \/\/.AddRedirect(\"([\/_0-9a-z-]+)+(.*)$\", \"\/?path=$1&ext=$2\", (int)HttpStatusCode.MovedPermanently); \/\/redirect any path that ends with .html \r\n \r\n            app.UseRewriter(options);\r\n\r\n            var routes = new RouteBuilder(app);\r\n            routes.MapGet(\"\", (context) =&gt; {\r\n                context.Response.Headers.Add(\"content-type\", \"text\/html\");\r\n                var path = context.Request.Query[\"Path\"];\r\n                var ext = context.Request.Query[\"Ext\"];\r\n                return context.Response.WriteAsync($@\"Always display this page when path ends with an extension (e.g. .html or .aspx) and capture the their values. \r\n                For example &lt;a href=\"\"\/hello-world.html\"\"&gt;\/hello-world.html&lt;\/a&gt; or &lt;a href=\"\"\/welcome\/everybody\/inthis\/train.aspx\"\"&gt;\/welcome\/everybody\/inthis\/train.aspx&lt;\/a&gt;.\r\n                &lt;br\/&gt;&lt;br\/&gt;\r\n                Query String \"\"path\"\" = {path}&lt;br\/&gt;\r\n                Query String \"\"ext\"\" = {ext}&lt;br\/&gt;\r\n                \");\r\n            });\r\n\r\n            app.UseRouter(routes.Build());\r\n        }\r\n    }\r\n\r\n    public class Program\r\n        {\r\n            public static void Main(string[] args)\r\n            {\r\n                CreateWebHostBuilder(args).Build().Run();\r\n            }\r\n\r\n        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =&gt;\r\n            WebHost.CreateDefaultBuilder(args)\r\n                .UseStartup&lt;Startup&gt;()\r\n                .UseEnvironment(\"Development\");\r\n        }\r\n}<\/pre>\n<p>\uacb0\uacfc : Always display this page when path ends with an extension (e.g. .html or .aspx) and capture the their values.For example \/hello-world.html or \/welcome\/everybody\/inthis\/train.aspx. <\/p>\n<p>Query String &#8220;path&#8221; = welcome\/everybody\/inthis\/train<br \/>\nQuery String &#8220;ext&#8221; = .aspx<\/p>\n","protected":false},"excerpt":{"rendered":"<p>using Microsoft.AspNetCore.Hosting; \/\/IWebHostBuilder using Microsoft.AspNetCore.Builder; \/\/IApplicationBuilder using Microsoft.AspNetCore.Http; \/\/WriteAsync using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Rewrite; \/\/UseRewriter using System.Net; namespace WebApplication1 { public class Startup { public Startup(IHostingEnvironment env, ILoggerFactory logger) { } public void ConfigureServices(IServiceCollection services) { \/\/This is the only service available at ConfigureServices services.AddRouting(); } public void Configure(IApplicationBuilder app,\u2026 <span class=\"read-more\"><a href=\"https:\/\/csharp.ihavenomoney.co.kr\/?p=659\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[108],"tags":[],"class_list":["post-659","post","type-post","status-publish","format-standard","hentry","category-net-core"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/659","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=659"}],"version-history":[{"count":4,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/659\/revisions"}],"predecessor-version":[{"id":663,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/659\/revisions\/663"}],"wp:attachment":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}