{"id":664,"date":"2019-09-26T14:16:21","date_gmt":"2019-09-26T05:16:21","guid":{"rendered":"http:\/\/csharp.ihavenomoney.co.kr\/?p=664"},"modified":"2019-09-26T14:16:21","modified_gmt":"2019-09-26T05:16:21","slug":"rewrite-5","status":"publish","type":"post","link":"https:\/\/csharp.ihavenomoney.co.kr\/?p=664","title":{"rendered":"rewrite-5"},"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;\r\nusing System.Net;\r\nusing Microsoft.Net.Http.Headers;\r\nusing System.IO;\r\n\r\n\r\nnamespace WebApplication1\r\n{\r\n\r\n    public class ExtensionRedirection : IRule\r\n    {\r\n        readonly string _extension;\r\n        readonly PathString _newPath;\r\n\r\n        public ExtensionRedirection(string extension, string newPath)\r\n        {\r\n            _extension = extension;\r\n            _newPath = new PathString(newPath);\r\n        }\r\n\r\n        public void ApplyRule(RewriteContext context)\r\n        {\r\n            var request = context.HttpContext.Request;\r\n\r\n            \/\/ Because we're redirecting back to the same app, stop processing if the request has already been redirected\r\n            \/\/ This is to prevent crazy loop. Try it, comment below code and you are going to crash.\r\n            if (request.Path.StartsWithSegments(new PathString(_newPath)))\r\n            {\r\n                return;\r\n            }\r\n\r\n            if (request.Path.Value.EndsWith(_extension, StringComparison.OrdinalIgnoreCase))\r\n            {\r\n                var response = context.HttpContext.Response;\r\n                response.StatusCode = StatusCodes.Status301MovedPermanently;\r\n                context.Result = RuleResult.EndResponse;\r\n                response.Headers[HeaderNames.Location] = _newPath + request.Path + request.QueryString;\r\n            }\r\n        }\r\n    }\r\n\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               .Add(new ExtensionRedirection(\".png\", \"\/images\/png\"))\r\n               .Add(new ExtensionRedirection(\".jpg\", \"\/images\/jpeg\"));\r\n\r\n            app.UseRewriter(options);\r\n\r\n            app.UseStaticFiles();\r\n\r\n            var routes = new RouteBuilder(app);\r\n            routes.MapGet(\"\", async 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                await context.Response.WriteAsync($\"&lt;h1&gt;Extension Based Redirection&lt;\/h1&gt;&lt;img src=\\\"ryan-wong-25025.jpg\\\" \/&gt; &lt;br\/&gt; &lt;img src=\\\"Acorn_PNG744.png\\\" \/&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 :<\/p>\n<p>Extension Based Redirection<br \/>\n[\uc774\ubbf8\uc9c0]  \/\/\uc0dd\ub7b5<br \/>\n[\uc774\ubbf8\uc9c0]  \/\/\uc0dd\ub7b5<\/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; using System.Net; using Microsoft.Net.Http.Headers; using System.IO; namespace WebApplication1 { public class ExtensionRedirection : IRule { readonly string _extension; readonly PathString _newPath; public ExtensionRedirection(string extension, string newPath) { _extension = extension; _newPath = new\u2026 <span class=\"read-more\"><a href=\"https:\/\/csharp.ihavenomoney.co.kr\/?p=664\">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-664","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\/664","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=664"}],"version-history":[{"count":1,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/664\/revisions"}],"predecessor-version":[{"id":665,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/664\/revisions\/665"}],"wp:attachment":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}