2010-07-27 52 views
0

有誰知道如何使用ASP.NET MVC重定向到另一個服務器/解決方案?這樣的事情:重定向到另一臺服務器 - ASP MVC

public void Redir(String param) 
{ 
    // Redirect to another application, ie: 
    // Redirect("www.google.com"); 
    // or 
    // Response.StatusCode= 301; 
    // Response.AddHeader("Location","www.google.com"); 
    // Response.End(); 

} 

我已經嘗試過上述兩種方式,但它不工作。

回答

3

RedirectResult會給你一個302,但是如果你需要一個301使用該結果類型:

public class PermanentRedirectResult : ActionResult 
{ 
    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 PermanentRedirectResult Redirect() 
{ 
    return new RedirectResult("http://www.google.com"); 
} 

源(因爲它不是我的工作):http://forums.asp.net/p/1337938/2700733.aspx

+0

+1從你從哪裏添加源。我可以欣賞這樣的行爲。 – XIII 2010-07-27 19:23:59

4
public ActionResult Redirect() 
    { 
     return new RedirectResult("http://www.google.com"); 
    } 

希望這有助於:-)

1

//這不是我的情況,所以我在這裏做了一些竅門。

public ActionResult Redirect() 
{ 
    return new PermanentRedirectResult ("http://www.google.com"); 
} 
+0

它嘗試在相同的域中重定向,如www.mysite.com/Home/www.google.com。你能補充說明嗎? – Maxim 2012-10-01 20:54:00

相關問題