title: Is middleware neeeded to redirect to HTTPS in ASP.net and C#?
body: <p>What is the recommend way to redirect to HTTPS all incoming requests that are not secure.  Do I need to write a middleware component?  If so, I couldn't figure out how to get the server name.</p>
  
  <pre><code>public class RedirectHttpMiddleware
  {
      RequestDelegate _next;
  
      public RedirectHttpMiddleware(RequestDelegate next)
      {
          _next = next;
      }
  
      public async Task Invoke(HttpContext context)
      {
          if (context.Request.IsSecure)
              await _next(context);
          else
          {
              var server = "";  // How do I get the server name?
              context.Response.Redirect("https://" + server + context.Request.Path);
          }
      }
  }
  </code></pre>