如何在 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");
}
一个更完整的答案将重定向到路线......301 重定向的正确控制器代码 https://stackoverflow.com/questions/1693548/correct-controller-code-for-a-301-redirect
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)