2010-07-27 42 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

相关问题