MVC 中301永久重定向

时间:2021-03-30 05:19:27

public class PermanentRedirectResult : ViewResult
{
public string Url { get; set; }

public PermanentRedirectResult(string url) 
{ 
    if (string.IsNullOrEmpty(url)) 
        throw new ArgumentException("url is null or empty", url); 
    this.Url = url; 
} 

public override void ExecuteResult(ControllerContext context) 
{ 
  if (context == null) 
    throw new ArgumentNullException("context"); 
  context.HttpContext.Response.StatusCode = 301; 
  context.HttpContext.Response.RedirectLocation = Url; 
  context.HttpContext.Response.End(); 
} 

}

使用
public class ProductController : Controller
{

    public ActionResult JingSuan()
    {
        return new PermanentRedirectResult("/Controller/Action/参数");
    }

}

MVC 中301永久重定向