[英]How to redirect .ASPX pages to .NET Core Razor page
We are moving a big asp.net web site to .NET Core Razor pages site.我们正在将一个大型 asp.net 网站迁移到 .NET Core Razor 页面站点。 All over the internet there are links that point to our site and we want those links to work after our migration.
整个互联网上都有指向我们网站的链接,我们希望这些链接在我们迁移后能够正常工作。 We will kept the same url format, but without the extension .aspx.
我们将保留相同的 url 格式,但没有扩展名 .aspx。
Summary, we want our old url:总结,我们想要我们的旧网址:
example.com/item.aspx be handle by .net core razor page, as example.com/item.aspx由 .net core razor 页面处理,如
example.com/item例子.com/item
You could use the URL Rewriting Middleware to remove the ".aspx" extensions.您可以使用URL 重写中间件删除“.aspx”扩展名。
Check the following code: Use AddRedirect to create a rule for rewriting URLs检查以下代码:使用AddRedirect创建重写 URL 的规则
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
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();
}
//using Regex match the .aspx extension and remove it.
var options = new RewriteOptions()
.AddRedirect(@"(\w*)(.aspx)", "$1");
app.UseRewriter(options);
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
Then, the screenshot like this:然后,截图是这样的:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.