重定向到另一个服务器 - ASP MVC [英] Redirect to another server - ASP MVC

查看:157
本文介绍了重定向到另一个服务器 - ASP MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人知道如何重定向到使用ASP.NET MVC另一台服务器/解决方案?事情是这样的:

Does anybody know how to redirect to another server/solution using ASP.NET MVC? Something like this:

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();

}

从来就试图左右逢源以上,但它doesn't的工作。

I´ve tried both ways above, but it doesn´t work.

推荐答案

RedirectResult 会给你一个302,但是如果你需要一个301使用这种结果类型:

The RedirectResult will give you a 302, however if you need a 301 use this result type:

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();
    }
} 

然后使用它像上面提到的:

Then use it like mentioned above:

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

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

这篇关于重定向到另一个服务器 - ASP MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆