如果 url 包含“https”,则重定向到静态 html 页面
接受警告“不可信(自使用 https)”之后。我想将用户重定向到带有一些有用消息的静态页面。
Https 连接不会失败,如果它是 Https 请求,我只是不想加载应用程序。我想改为加载静态 Html 页面。
例如:https://localhost:1234 然后重定向到静态页面
现在我有什么:
如果 url 是 http://localhost:3221它重定向到 HomeController -> Index Action
更像是为Https请求添加常规路由,不知道可不可以。
最佳答案
您可以为此编写一个简单的中间件。像这样的东西应该可以完成这项工作:
app.Use(async (context, next) =>
{
// check if the request is *not* using the HTTPS scheme
if (!context.Request.IsHttps)
{
// redirect to some relative URL (see note below!)
context.Response.Redirect("/no-https-error.html");
return;
}
// otherwise continue with the request pipeline
await next();
});
因为这是 a middleware它会影响在您插入管道时到达中间件的每个请求。因此,如果您将它放在 app.UseStaticFiles()
之前,那么这个中间件也会阻止您访问静态文件,包括您重定向到的错误文件。如果你想这样做,你必须让重定向包含一个指向 http://
地址的绝对路径:
var url = "http://" + context.Request.Host + context.Request.PathBase + "/no-https-error.html";
context.Response.Redirect(url);
关于Asp.net core,如果 URL 有 Https,则重定向到静态 html 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48511185/