DotNetCore 1.0 MVC 如何在直播中自动重定向到单个域
DotNetCore 1.0 MVC how to automatically redirect to a single domain in live
我有一个网站的多个域:
在生产中,我希望主域为 http://www.example.com,所有其他关联域自动重定向到主域。
过去,我会使用 URLRewrite 完成此操作,但是我被引导相信 DotNetCore 中不存在。
那么...我该怎么做?
此外,我不希望这影响开发环境。
您可以在 .NET Core 中使用 URLRewrite - 只需下载 ASP.NET Core 1.1 Preview 1。然后您就可以:
public class Startup {
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
var options = new RewriteOptions()
.AddRedirect("(.*)/$", "") // Redirect using a regular expression
.AddRewrite(@"app/(\d+)", "app?id=", skipRemainingRules: false) // Rewrite based on a Regular expression
.AddRedirectToHttps(302, 5001) // Redirect to a different port and use HTTPS
.AddIISUrlRewrite(env.ContentRootFileProvider, "UrlRewrite.xml") // Use IIS UrlRewriter rules to configure
.AddApacheModRewrite(env.ContentRootFileProvider, "Rewrite.txt"); // Use Apache mod_rewrite rules to configure
app.UseRewriter(options);
}
}
要将现有项目更新到 ASP.NET Core 1.1 Preview 1,您需要执行以下操作:
- 下载并安装更新的 .NET Core 1.1 Prevew 1 SDK
- 按照 .NET Core 1.1 预览版 1 公告中的说明更新您的项目以使用 .NET Core 1.1 预览版 1
- 更新您的 ASP.NET 核心包依赖项以使用新的 1.1.0-preview1 版本
更多信息在这里:https://blogs.msdn.microsoft.com/webdev/2016/10/25/announcing-asp-net-core-1-1-preview-1/
适用于 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();
}
});
}
}
在 .net 核心 6 中:
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
var options = new RewriteOptions()
.Add(RewriteRules.ReDirectRequestsToOneHost);
app.UseRewriter(options);
}
public class RewriteRules
{
public static void ReDirectRequestsToOneHost(RewriteContext context)
{
var request = context.HttpContext.Request;
if (request.Host != new HostString("www.example.com"))
{
var redirect = $"{request.Scheme}://www.example.com{request.Path}";
context.HttpContext.Response.Redirect(redirect);
}
}
}
我有一个网站的多个域:
在生产中,我希望主域为 http://www.example.com,所有其他关联域自动重定向到主域。
过去,我会使用 URLRewrite 完成此操作,但是我被引导相信 DotNetCore 中不存在。
那么...我该怎么做?
此外,我不希望这影响开发环境。
您可以在 .NET Core 中使用 URLRewrite - 只需下载 ASP.NET Core 1.1 Preview 1。然后您就可以:
public class Startup {
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
var options = new RewriteOptions()
.AddRedirect("(.*)/$", "") // Redirect using a regular expression
.AddRewrite(@"app/(\d+)", "app?id=", skipRemainingRules: false) // Rewrite based on a Regular expression
.AddRedirectToHttps(302, 5001) // Redirect to a different port and use HTTPS
.AddIISUrlRewrite(env.ContentRootFileProvider, "UrlRewrite.xml") // Use IIS UrlRewriter rules to configure
.AddApacheModRewrite(env.ContentRootFileProvider, "Rewrite.txt"); // Use Apache mod_rewrite rules to configure
app.UseRewriter(options);
}
}
要将现有项目更新到 ASP.NET Core 1.1 Preview 1,您需要执行以下操作:
- 下载并安装更新的 .NET Core 1.1 Prevew 1 SDK
- 按照 .NET Core 1.1 预览版 1 公告中的说明更新您的项目以使用 .NET Core 1.1 预览版 1
- 更新您的 ASP.NET 核心包依赖项以使用新的 1.1.0-preview1 版本
更多信息在这里:https://blogs.msdn.microsoft.com/webdev/2016/10/25/announcing-asp-net-core-1-1-preview-1/
适用于 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();
}
});
}
}
在 .net 核心 6 中:
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
var options = new RewriteOptions()
.Add(RewriteRules.ReDirectRequestsToOneHost);
app.UseRewriter(options);
}
public class RewriteRules
{
public static void ReDirectRequestsToOneHost(RewriteContext context)
{
var request = context.HttpContext.Request;
if (request.Host != new HostString("www.example.com"))
{
var redirect = $"{request.Scheme}://www.example.com{request.Path}";
context.HttpContext.Response.Redirect(redirect);
}
}
}