title: .Net Core 2.x Redirect to Error Page
body: <p>I have a simple ASP.Net Core 2.0 web application and I have enabled Windows Authentication in project property by enabling Windows Authentication and disabled Anonymous Authentication.</p>
  
  <p>For Authorization at Application/Site level filtering by AD security group, I have the following code in Startup.cs:</p>
  
  
  
  <pre class="lang-cs prettyprint-override"><code>public class Startup
  {
      public Startup(IConfiguration configuration)
      {
          Configuration = configuration;
      }
  
      public IConfiguration Configuration { get; }
  
      // This method gets called by the runtime. Use this method to add services to the container.
      public void ConfigureServices(IServiceCollection services)
      {
          services.AddMvc();
  
          services.AddAuthentication(IISDefaults.AuthenticationScheme);
  
          services.AddMvc(config =&gt;
          {
              var policy = new AuthorizationPolicyBuilder()
                                  .RequireAuthenticatedUser()
                                  .RequireRole("Application - Administrator")
                                  .Build();
              config.Filters.Add(new AuthorizeFilter(policy));
          });
      }
  
      // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
      public void Configure(IApplicationBuilder app, IHostingEnvironment env)
      {
          if (env.IsDevelopment())
          {
              app.UseBrowserLink();
              app.UseDeveloperExceptionPage();
          }
          else
          {
              app.UseExceptionHandler("/Home/Error");
          }
  
          app.UseStaticFiles();
  
          app.UseAuthentication();
  
          //app.UseStatusCodePagesWithRedirects("/Home/Error/{0}");
          //app.UseStatusCodePagesWithReExecute("/Home/Error", "?statusCode={0}");
  
          app.UseStatusCodePagesWithReExecute("/Home/Error/{0}");
  
  
  
          app.UseMvc(routes =&gt;
          {
              routes.MapRoute(
                  name: "default",
                  template: "{controller=Home}/{action=Index}/{id?}");
          });
      }
  }
  </code></pre>
  
  <p>To handle Http 403 status code when a un-authorized user tries to access the application, it will be redirect to a custom error page. So I tried the following 3 approaches in Configure method within Startup.cs:</p>
  
  <pre class="lang-cs prettyprint-override"><code>app.UseStatusCodePagesWithRedirects("/Home/Error/{0}");
  app.UseStatusCodePagesWithReExecute("/Home/Error", "?statusCode={0}");
  app.UseStatusCodePagesWithReExecute("/Home/Error/{0}");
  </code></pre>
  
  <p>In the HomeController, I tried both the default Error method</p>
  
  <pre class="lang-cs prettyprint-override"><code>public IActionResult Error()
  {
      return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
  }
  </code></pre>
  
  <p>and customized one to handle specific status code:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public IActionResult Error(string errCode)
  {
      if (errCode == "500" || errCode == "404" || errCode == "403")
      {
          return View($"~/Views/Error/{errCode}.cshtml");
      }
  
      return View("~/Views/Shared/Error.cshtml");
  }
  </code></pre>
  
  <p>And I have a simple error page 403.cshtml under /Views/Error/ folder.</p>
  
  <p>But none of them works, all display this page:
  <a href="https://i.stack.imgur.com/kkFOl.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kkFOl.jpg" alt="enter image description here"></a></p>
  
  <p>I am wondering if something I missed or forgot to implement for display a formatted error page?</p>
  
  <p>Thanks in advance.</p>