title: Redirect in Asp.Net Core middleware does not render target page
body: <p>I added a middleware item (called CDASMiddleware) in Startup.cs that identifies 4xx http errors and does a redirection to a error handler that logs the error to the database and displays
  a custom error page.</p>
  
  <p>As an example, lets assume I wish to track 404 errors.  The Invoke method of the middleware handles that error with the following code.</p>
  
  <pre><code>public async Task Invoke(HttpContext httpContext)
  {
       await _next(httpContext); 
       if (httpContext.Response.StatusCode == 404)
       {
           var message = $@"404 error. URL: {httpContext.Request.Path.Value}   was not found.";
           Error e = new Error() { StatusCode = "404", Message = message };
           var obj = JsonConvert.SerializeObject(e);
           httpContext.Session.SetString("HttpError", obj);
           var url = httpContext.Request.PathBase + "/Error/HttpError";
           httpContext.Response.Redirect(url, true);
      }
  }
  </code></pre>
  
  <p>And, indeed, when this code is called, the corresponding controller method is called which successfully stores the error in the database and then returns the associated view.</p>
  
  <pre><code> public IActionResult HttpError() 
       {
           var session = this.HttpContext.Session;
           Error e = JsonConvert.DeserializeObject&lt;Error&gt;(session.GetString("HttpError"));
           var el = GetErrorLogFromError(e); //create error log entry for db
           _dataContext.ErrorLogDataSet.Upsert(el);   
           return View("~/Views/Shared/Error.cshtml", e);
       }
  </code></pre>
  
  <p>Upon doing a F12 debug in the browser, as expected, I see a 302 (have tried 301 as well) associated with the redirect and a subsequent 200 response with the correct html view in the http body. So the browser gets the correct view.  But it is not rendered and the browser url does not change to reflect the redirection.</p>
  
  <p>Oh, and the Configure method in Startup.cs is</p>
  
  <pre><code>public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ICDASDataContext dataContext)
          {
              app.UseSession();
              app.UseCDASMiddleware();
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage(); //show detailed error page to developer
                  app.UseBrowserLink();
              }
              else
              {
                  app.UseExceptionHandler("/CDASUnhandledException");  //log to the database and show a custom error page to user for unhandled exceptions
                  Func&lt;string, LogLevel, bool&gt; f = ((s, logLevel) =&gt; logLevel &gt;= LogLevel.Error); //log handled errors
                  loggerFactory.AddProvider(new CDASLoggerProvider(f, dataContext));
              }
              app.UseStaticFiles();
              app.UseMvc(routes =&gt;
              {
                  routes.MapRoute(
                      name: "AreaRoute",
                      template: "{area:exists}/{controller}/{action}",
                      defaults: new { controller = "Home", action = "Index" });
  
                  routes.MapRoute(
                      name: "default",
                      template: "{controller=Home}/{action=Index}/{id?}");
  
  
              });
          }
  </code></pre>
  
  <p>Any ideas?</p>