你如何做一个301您永久在ASP.Net MVC重定向路径 [英] How do you do a 301 permanant redirect route in ASP.Net MVC

查看:205
本文介绍了你如何做一个301您永久在ASP.Net MVC重定向路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么做一个HTTP 301您永久的ASP.NET MVC重定向路径?

How do you do a HTTP 301 permanant redirect route in ASP.NET MVC?

推荐答案

创建的ActionResult ...

Create a class that inherits from ActionResult...



    public class PermanentRedirectResult : ActionResult
    {    
        public string Url { get; set; }

        public PermanentRedirectResult(string url)
        {
            this.Url = url;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
            context.HttpContext.Response.RedirectLocation = this.Url;
            context.HttpContext.Response.End();
        }
    }

然后用它...

Then to use it...



        public ActionResult Action1()
        {          
            return new PermanentRedirectResult("http://stackoverflow.com");
        }



一个更完整的答案将重定向到路线... <一个href=\"http://stackoverflow.com/questions/1693548/correct-controller-$c$c-for-a-301-redirect\">http://stackoverflow.com/questions/1693548/correct-controller-$c$c-for-a-301-redirect

A more complete answer that will redirect to routes... http://stackoverflow.com/questions/1693548/correct-controller-code-for-a-301-redirect

这篇关于你如何做一个301您永久在ASP.Net MVC重定向路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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