您如何在 ASP.NET MVC 中执行 HTTP 301 永久重定向路由?
最佳答案
创建一个继承自 ActionResult 的类...
public class PermanentRedirectResult : ActionResult
{
public string Url { get; set; }
public PermanentRedirectResult(string url)
{
this.Url = url;
}
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
context.HttpContext.Response.RedirectLocation = this.Url;
context.HttpContext.Response.End();
}
}
然后使用它...
public ActionResult Action1()
{
return new PermanentRedirectResult("http://stackoverflow.com");
}
一个更完整的答案将重定向到路由... Correct Controller code for a 301 Redirect
关于asp.net - 如何在 ASP.Net MVC 中执行 301 永久重定向路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2216890/