From 0589b883b891d8179fb166c663c8625c827a533c Mon Sep 17 00:00:00 2001 From: Chris R Date: Tue, 12 Jan 2016 13:33:21 -0800 Subject: [PATCH] #14, 52 Add IWebApplicationBuilder.UseIISPlatformHandlerUrl, include path base --- samples/IISSample/Startup.cs | 10 +++++ .../IISPlatformHandlerAddressExtensions.cs | 38 +++++++++++++++++++ .../project.json | 5 ++- test/TestSites/Program.cs | 1 + 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 src/Microsoft.AspNet.IISPlatformHandler/IISPlatformHandlerAddressExtensions.cs diff --git a/samples/IISSample/Startup.cs b/samples/IISSample/Startup.cs index e22705f651..c177ee9778 100644 --- a/samples/IISSample/Startup.cs +++ b/samples/IISSample/Startup.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; @@ -23,10 +24,18 @@ namespace IISSample context.Response.ContentType = "text/plain"; await context.Response.WriteAsync("Hello World - " + DateTimeOffset.Now + Environment.NewLine); await context.Response.WriteAsync("User - " + context.User.Identity.Name + Environment.NewLine); + await context.Response.WriteAsync("PathBase: " + context.Request.PathBase.Value + Environment.NewLine); + await context.Response.WriteAsync("Path: " + context.Request.Path.Value + Environment.NewLine); foreach (var header in context.Request.Headers) { await context.Response.WriteAsync(header.Key + ": " + header.Value + Environment.NewLine); } + var vars = Environment.GetEnvironmentVariables(); + foreach (var key in vars.Keys) + { + var value = vars[key]; + await context.Response.WriteAsync(key + ": " + value + Environment.NewLine); + } }); } @@ -34,6 +43,7 @@ namespace IISSample { var application = new WebApplicationBuilder() .UseConfiguration(WebApplicationConfiguration.GetDefault(args)) + .UseIISPlatformHandlerUrl() .UseStartup() .Build(); diff --git a/src/Microsoft.AspNet.IISPlatformHandler/IISPlatformHandlerAddressExtensions.cs b/src/Microsoft.AspNet.IISPlatformHandler/IISPlatformHandlerAddressExtensions.cs new file mode 100644 index 0000000000..b166bcc6a6 --- /dev/null +++ b/src/Microsoft.AspNet.IISPlatformHandler/IISPlatformHandlerAddressExtensions.cs @@ -0,0 +1,38 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; + +namespace Microsoft.AspNet.Hosting +{ + public static class IISPlatformHandlerAddressExtensions + { + // This is defined by IIS's HttpPlatformHandler. + private static readonly string ServerPort = "HTTP_PLATFORM_PORT"; + private static readonly string ServerPath = "HTTP_PLATFORM_APPL_PATH"; + + /// + /// Configures the port and base path the server should listen on when running behind HttpPlatformHandler. + /// + /// + /// + public static IWebApplicationBuilder UseIISPlatformHandlerUrl(this IWebApplicationBuilder app) + { + if (app == null) + { + throw new ArgumentNullException(nameof(app)); + } + + var port = Environment.GetEnvironmentVariable(ServerPort); + var path = Environment.GetEnvironmentVariable(ServerPath); + + if (!string.IsNullOrEmpty(port)) + { + var address = "http://localhost:" + port + path; + app.UseSetting(WebApplicationDefaults.ServerUrlsKey, address); + } + + return app; + } + } +} diff --git a/src/Microsoft.AspNet.IISPlatformHandler/project.json b/src/Microsoft.AspNet.IISPlatformHandler/project.json index e00f2aa8e2..e9a6179e42 100644 --- a/src/Microsoft.AspNet.IISPlatformHandler/project.json +++ b/src/Microsoft.AspNet.IISPlatformHandler/project.json @@ -10,13 +10,14 @@ "url": "git://github.com/aspnet/IISIntegration" }, "dependencies": { + "Microsoft.AspNet.Hosting.Abstractions": "1.0.0-*", "Microsoft.AspNet.Http": "1.0.0-*", "Microsoft.AspNet.Http.Extensions": "1.0.0-*", + "Microsoft.Extensions.Options": "1.0.0-*", "Microsoft.Extensions.SecurityHelper.Sources": { "type": "build", "version": "1.0.0-*" - }, - "Microsoft.Extensions.Options": "1.0.0-*" + } }, "frameworks": { "net451": { }, diff --git a/test/TestSites/Program.cs b/test/TestSites/Program.cs index fa4dbb25c8..3f98c54769 100644 --- a/test/TestSites/Program.cs +++ b/test/TestSites/Program.cs @@ -11,6 +11,7 @@ namespace TestSites { var application = new WebApplicationBuilder() .UseConfiguration(WebApplicationConfiguration.GetDefault(args)) + .UseIISPlatformHandlerUrl() .UseStartup("TestSites") .Build();