我有一个网站的多个域:
在生产中,我希望主域是 http://www.example.com并让所有其他关联域自动重定向到主域。
从历史上看,我会使用 URLRewrite 完成此操作,但是我被引导相信 DotNetCore 中不存在。
那么...我该怎么做呢?
此外,我不希望这影响开发环境。
最佳答案
适用于 DotNetCore 1.0 的答案(也强制使用 https)
Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// Other configuration code here...
if (env.IsProduction())
{
app.Use(async (context, next) =>
{
if (context.Request.Host != new HostString("www.example.com"))
{
var withDomain = "https://www.example.com" + context.Request.Path;
context.Response.Redirect(withDomain);
}
else if (!context.Request.IsHttps)
{
var withHttps = "https://" + context.Request.Host + context.Request.Path;
context.Response.Redirect(withHttps);
}
else
{
await next();
}
});
}
}
关于c# - DotNetCore 1.0 MVC 如何在直播中自动重定向到单个域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40305312/