【问题标题】:404 error handling for HTTPS?HTTPS 的 404 错误处理?
【发布时间】:2017-03-02 01:25:07
【问题描述】:

我对 ASP.NET Core 非常陌生,正在尝试构建我的第一个网站。我在错误处理方面苦苦挣扎,真的需要一些建议。

为了处理 404 错误,我使用了代码 app.UseStatusCodePagesWithReExecute("/Home/Error/{0}"); 在 startup.cs 中

带有家庭控制器

    [HttpGet("Home/error/{errcode}")]
    public IActionResult Error(int errCode)
    {         
        return View("Error", errCode);            
    }  

这似乎成功地捕获了 404,500 个错误并显示带有正确代码的错误视图页面。错误视图在/Views/Shared/Error.cshtml

但是当我像这样在 startup.cs 中添加从 HTTP 到 HTTPS 的重定向时

        app.Use(async (context, next) =>
        {
            if (context.Request.IsHttps)
            {
                await next();
            }
            else
            {
                var httpsUrl = "https://" + context.Request.Host + context.Request.Path;
                context.Response.Redirect(httpsUrl);
            }
        });

错误处理将不再起作用,并在 Firefox 中显示 Secure Connection Failed Error code: SSL_ERROR_RX_RECORD_TOO_LONG

现在看起来像这样。

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();
          if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error/{0}");
            // StatusCodePagesMiddleware to handle errors     
            app.UseStatusCodePagesWithReExecute("/Home/Error/{0}");
        }
        app.UseStaticFiles();
        app.UseIdentity();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=LandingPage}/{id?}");               
        });

        app.Use(async (context, next) =>
        {
            if (context.Request.IsHttps)
            {
                await next();
            }
            else
            {
                var httpsUrl = "https://" + context.Request.Host + context.Request.Path;
                context.Response.Redirect(httpsUrl);
            }
        });
     }

请问如何处理 ASP.NET MVC Core1.0 中的 HTTPS 错误?

非常感谢您!

【问题讨论】:

  • 能否发布您的配置属性?
  • 你是这个意思吗? { "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:53519/", "sslPort": 0 } },
  • 更改 sslport 的值并重试。在我的工作环境中设置为 44300 并且可以完美运行
  • 刚刚尝试更改为 "sslPort": 44300 但仍然不行。

标签: c# asp.net-core http-status-code-404 asp.net-core-mvc


【解决方案1】:

如果中间件很重要,请订购。你应该先做重定向,然后使用 MVC:

app.Use(async (context, next) =>
{
    // redirect
    ...
});

app.UseMvc(routes =>
{
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    • 2019-08-11
    • 2015-10-31
    • 2015-04-07
    • 2013-12-08
    • 2013-07-05
    • 2012-11-16
    相关资源
    最近更新 更多