C# 在MVC中重定向来自旧网站的请求
我已经使用ASP.NETMVC3为一个客户机构建了一个网站,该网站的构建是为了替换一个我没有构建的、用PHP编写的旧网站 我在新网站地图上看到的大多数页面都是从原来的页面改为旧的页面,比如www.mysite.com/contactus,以前是www.mysite.com/contactus.php 在查看我的错误日志(由Elmah记录)后,我发现一些旧页面的请求出现错误,如下所示:C# 在MVC中重定向来自旧网站的请求,c#,asp.net-mvc,redirect,C#,Asp.net Mvc,Redirect,我已经使用ASP.NETMVC3为一个客户机构建了一个网站,该网站的构建是为了替换一个我没有构建的、用PHP编写的旧网站 我在新网站地图上看到的大多数页面都是从原来的页面改为旧的页面,比如www.mysite.com/contactus,以前是www.mysite.com/contactus.php 在查看我的错误日志(由Elmah记录)后,我发现一些旧页面的请求出现错误,如下所示: public class PhpRedirectController : Controller { [H
public class PhpRedirectController : Controller
{
[HttpGet]
public ActionResult Get(string path)
{
// TryGetNewUrl should be implemented to perform the
// mapping, or return null is there is none.
string newUrl = TryGetNewUrl(path);
if (newUrl == null)
{
// 404 not found
return new HttpNotFoundResult();
}
else
{
// 301 moved permanently
return RedirectPermanent(newUrl);
}
}
}
路径“/ContactUs.php”的控制器未找到或未实现IController
是否有人对如何纠正此问题提出建议,最好是将用户重定向到新的目标(如果存在),或者只是将其默认为主页。您应该能够使用web.config中的IIS重写规则来完成此操作:
<rewrite>
<rules>
<rule name="Remove .php suffix">
<match url="^(.*).php$" />
<action type="Rewrite" url="{R:1}" />
</rule>
</rules>
</rewrite>
您可以使用以下路线:
routes.MapRoute(
name: "oldphp",
url: "{*path}",
defaults: new { controller = "PhpRedirect", action="Get" },
constraints: new { path = @".*\.php" });
然后像这样实现PhpRedirectController
:
public class PhpRedirectController : Controller
{
[HttpGet]
public ActionResult Get(string path)
{
// TryGetNewUrl should be implemented to perform the
// mapping, or return null is there is none.
string newUrl = TryGetNewUrl(path);
if (newUrl == null)
{
// 404 not found
return new HttpNotFoundResult();
}
else
{
// 301 moved permanently
return RedirectPermanent(newUrl);
}
}
}
@我已经更新了我的答案,所以在这种情况下也能用。