Asp.net mvc 重定向到另一个服务器-ASP MVC
有人知道如何使用ASP.NET MVC重定向到其他服务器/解决方案吗?大概是这样的:Asp.net mvc 重定向到另一个服务器-ASP MVC,asp.net-mvc,redirect,Asp.net Mvc,Redirect,有人知道如何使用ASP.NET 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");
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();
}
我试过以上两种方法,但都不起作用
public ActionResult Redirect()
{
return new RedirectResult("http://www.google.com");
}
希望这有帮助:-)重定向结果将为您提供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");
}
来源(因为这不是我的工作)://在我的情况下它不起作用,所以我在这里做了一些技巧
public ActionResult Redirect()
{
return new PermanentRedirectResult ("http://www.google.com");
}
+1用于添加从何处获取的源。我很欣赏这种行为。它试图在同一个域中重定向,比如www.mysite.com/Home/www.google.com。你能补充一些关于这方面的澄清吗?