Redirect to another server - ASP MVC

后端 未结 3 1332
野趣味
野趣味 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

提交回复
热议问题