[英]How redirect to MVC routes using a old asp site url correctly?
让我解释。 最近将一个经典的asp应用迁移到.net MVC4。 旧站点由很多 html 和 aspx 页面组成,即:
自然地,旧的 asp 站点是通过www.library.com/index.html、www.library.com/book.html、www.library.com/contactus.aspx等 URL 访问的。
现在,通过路由器访问当前 MVC 站点,即www.library.com/Home、www.library.com/Book、www.library.com/Contact 。
很多公司客户尝试使用旧 URL 访问新站点,这是一个问题,我需要从代码中找到解决方案。
我尝试了一些解决方案,但不起作用。
解决方案一
对于旧的 aspxs 页面,可以在新站点根目录上创建与旧站点同名的 aspx 页面,并从 codebehind 在 Page_Load 上重定向
//Create contactus.aspx on root directory project and redirect
public partial class Contact : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.RedirectToRoute(new { controller = "Contact", action = "Index" });
}
}
对于 html 页面,您可以在元标记上定义简单的刷新
//Create a html page, i.e., index.html
<meta http-equiv="refresh" content="0; url=http://library.com/Home" />
<p><a href="http://example.com/">Redirect</a></p>
这不是一个可行的解决方案,它需要为每个需要的重定向创建一个 html 页面或 aspx 页面。
解决方案 2
我尝试在 Global.asax 上定义重定向条件,但不起作用
protected void Application_BeginRequest(object sender, EventArgs e )
{
string url = Request.Url.ToString().ToLower();
if (url.Contains("index.html"))
Context.Response.RedirectPermanent("/Home");
else if (url.Contains("book.html"))
Context.Response.RedirectPermanent("/Book");
else if (url.Contains("contactus.aspx"))
Context.Response.RedirectPermanent("/Contact");
}
解决方案 3
我尝试在 RouteConfig.cs 上为重定向定义映射,但也不起作用
//Trying redirect on RegisterRoutes() in RouteConfig.cs
routes.MapRoute(
name: "Home",
url: "index.html",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Book",
url: "book.html",
defaults: new { controller = "Book", action = "Index" }
);
routes.MapRoute(
name: "Contact",
url: "contactus.aspx",
defaults: new { controller = "Contact", action = "Index" }
);
当用户尝试正确访问旧的 asp 站点 url 时如何重定向到新的 mvc 路由? 需要找到一个快速和经济的解决方案,感谢您的帮助。
您需要基于正则表达式模式的 Url Rewrite。 参考这个线程,OP(@Eilimint)已经更新了内联解决方案。 这可能是一个优雅的解决方案,允许我们在不更改代码的情况下进行覆盖。
<rule name="Redirect to new domain" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" matchType="Pattern" pattern="^olddomain(:\d+)?$" />
</conditions>
<action type="Redirect" url="https://my.newdomain.io/{R:1}" redirectType="Permanent" />
</rule>
多年来,我一直在努力寻找将旧 html 页面重定向到 asp.net 路由的解决方案。 解决方案 3 在页面位于根级别时效果很好,但在子文件夹中并不那么容易。 此外,根据新站点的结构,一个路由重定向可能不会覆盖所有 html 页面。
我终于放弃了使用 meta http-equiv="refresh" 创建原始 html 页面:
原始页面: https ://oldsite.com/pages/subpages/old.html
新页面: https ://newsite.com/pages/subpages/old.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="refresh" content="7; url='https://newsite.com/newcontroller/action'" />
</head>
<body>
<p>Please follow <a ref="https://newsite.com/newcontroller/action">this link</a>.</p>
</body>
</html>
我知道这个问题被问到已经有一段时间了,但我有类似的要求。 只是张贴以防有人来看。
我需要将 aspx 页面(直接访问时)重定向到在 RouteTable 集合中配置的友好名称。 我通过在 global.asax.vb 的 Application_BeginRequest 中调用以下代码来使其工作
If Request.FilePath.IndexOf(".aspx", StringComparison.OrdinalIgnoreCase) > -1 Then
Dim rh As PageRouteHandler
For Each r As Route In RouteTable.Routes.OfType(Of Route)
Try
If TypeOf (r.RouteHandler) Is PageRouteHandler Then
rh = r.RouteHandler
If rh.VirtualPath.Contains(Request.FilePath) Then
Response.Redirect(r.Url)
End If
End If
Catch ex As Exception
End Try
Next
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.