asp.net - 如何在 ASP.Net MVC 中执行 301 永久重定向路由

标签 asp.net asp.net-mvc redirect http-status-code-301 routes

您如何在 ASP.NET MVC 中执行 HTTP 301 永久重定向路由?

最佳答案

创建一个继承自 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(); } }
然后使用它...
public ActionResult Action1() { return new PermanentRedirectResult("http://stackoverflow.com"); }
一个更完整的答案将重定向到路由... Correct Controller code for a 301 Redirect

关于asp.net - 如何在 ASP.Net MVC 中执行 301 永久重定向路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2216890/

相关文章:

asp.net - DropDownList 的 MVC2 编辑器模板

asp.net-mvc - 如何在 Azure 上已托管的 Web API 旁边添加 MVC 项目?

c# - 从 MVC 中的 Controller 构造函数中删除依赖项

asp.net-mvc - MVC 3 Webgrid - 如何隐藏不希望可见的列?

javascript - 使用jquery查找中继器复选框选中的值

c# - 呈现时如何从 WebControl 中删除 span 标记

asp.net - 如何从 Windows 应用程序编程(使用 C#.NET 的 WinForms)迁移到 Web 应用程序编程(ASP.NET)?

laravel - Go 可以轻松模拟 Laravel 返回 Redirect::back() 的行为吗?

javascript - React-Router 外部链接

php - 在 PHP 中传递错误消息