Redirect to another server - ASP MVC

后端 未结 3 1296
野趣味
野趣味 2021-01-07 14:44

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

public void Redir(String param)
{
   // Redirect to anot         


        
相关标签:
3条回答
  • 2021-01-07 15:21

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

    Source (as it's not my work): http://forums.asp.net/p/1337938/2700733.aspx

    0 讨论(0)
  • 2021-01-07 15:21
        public ActionResult Redirect()
        {
            return new RedirectResult("http://www.google.com");
        }
    

    hope this helps :-)

    0 讨论(0)
  • 2021-01-07 15:26

    // It was not working in my case so I did some trick here.

    public ActionResult Redirect()
    {
         return new PermanentRedirectResult ("http://www.google.com");
    }
    
    0 讨论(0)
提交回复
热议问题