2010-02-07 41 views

Trả lời

2

Bạn muốn chuyển hướng 301, a 302 is temporary, a 301 is permanent. Trong ví dụ này, context là HttpContext:

context.Response.Status = "301 Moved Permanently"; 
context.Response.StatusCode = 301; 
context.Response.AppendHeader("Location", nawPathPathGoesHere); 
+3

Dòng đầu tiên là không cần thiết, vì StatusCode cũng sẽ đặt nhãn thích hợp. Trạng thái không được chấp nhận. –

8

Tạo một lớp kế thừa từ 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(); 
     } 
    } 
 

Sau đó, để sử dụng nó ...

 

     public ActionResult Action1() 
     {   
      return new PermanentRedirectResult("http://stackoverflow.com"); 
     } 


 

Một hoàn chỉnh hơn câu trả lời sẽ chuyển hướng đến các tuyến đường ... Correct Controller code for a 301 Redirect

+0

nếu tôi cố gắng chuyển hướng các tệp .html cũ không còn tồn tại trong? tôi có thể sử dụng định tuyến để xử lý chúng không? Cách tiếp cận chung là gì? – Rich

+0

Tôi có thể đi với một số tuyến đường tùy chỉnh như http://blog.eworldui.net/post/2008/04/ASPNET-MVC---Legacy-Url-Routing.aspx hoặc tôi chưa sử dụng mô-đun http với một cấu hình riêng biệt để bạn có thể dễ dàng loại bỏ và đăng nhập. http://www.hanselman.com/blog/ASPNETMVCAndTheNewIIS7RewriteModule.aspx – JKG

+1

Đã chuyển hướng RedirectPermanent bằng mvc. Hãy xem http://stackoverflow.com/a/16980631/532517 –

Các vấn đề liên quan