【问题标题】:Permanent Redirect Legacy Routes for static files in ASP.Net MVCASP.Net MVC 中静态文件的永久重定向传统路由
【发布时间】:2011-06-07 14:02:27
【问题描述】:

我们的旧 ASP.net 站点将静态图像存储在根目录下名为 /images 的子目录中。

我们新的 ASP.net MVC 站点将这些图像存储在 /Content/Images

的新布局中

我已更改站点中的所有页面以适应新的文件夹结构,但我想设置从旧静态图像到新位置的永久重定向。

我们的网站是托管的,我无法控制 IIS,那么解决这个问题的最佳方法是什么?

【问题讨论】:

  • 事实证明,我的虚拟主机确实让我可以控制 IIS,因此我能够使用 Url Rewriting 模块来完成我的要求。但是,如果社区为那些处于我认为我所处的情况的人提供答案,我会留下这个问题。

标签: asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-routing


【解决方案1】:

我为我的 MVC 2 网站使用以下代码:

// The legacy route class that exposes a RedirectActionName
public class LegacyRoute : Route
{
    public LegacyRoute(string url, string redirectActionName, IRouteHandler routeHandler)
        : base(url, routeHandler)
    {
        RedirectActionName = redirectActionName;
        Defaults = new RouteValueDictionary(new { controller = "Home", action = "Index"}); // is not actually called
    }

    public string RedirectActionName { get; set; }
}

// The legacy route handler, used for getting the HttpHandler for the request
public class LegacyRouteHandler : MvcRouteHandler
{
    protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        requestContext.HttpContext.Response.Write("success");
        return new LegacyHandler(requestContext);
    }
}

// The legacy HttpHandler that handles the request
public class LegacyHandler : MvcHandler
{
    public LegacyHandler(RequestContext requestContext) : base(requestContext)
    {
        requestContext.HttpContext.Response.Write("success");
        ProcessRequest(requestContext.HttpContext);
    }

    protected override void ProcessRequest(HttpContextBase httpContext)
    {
        string redirectActionName = ((LegacyRoute) RequestContext.RouteData.Route).RedirectActionName;
        var route = new Route(redirectActionName, ((LegacyRoute)RequestContext.RouteData.Route).Defaults, new MvcRouteHandler());

        // Copy all of the querystring parameters and put them within RouteContext.RouteData.Values
        var values = new Dictionary<string, object>();
        foreach (var s in RequestContext.RouteData.Values)
        {
            values.Add(s.Key, s.Value);
        }
        foreach (var s in httpContext.Request.QueryString.AllKeys)
        {
            values.Add(s, httpContext.Request.QueryString[s]);
        }
        var data = route.GetVirtualPath(RequestContext, new RouteValueDictionary(values));

        httpContext.Response.Status = "301 Moved Permanently";
        httpContext.Response.AppendHeader("Location", "/" + data.VirtualPath + "/");
        httpContext.Response.End();
    }
}

然后我只需将旧路线添加到我的路线图:

routes.Insert(13, new LegacyRoute("search", "search/{query}", new LegacyRouteHandler()));

【讨论】:

  • +1 您如何使用此代码?您在哪里为 LegacyHandler、MVC3 创建 .cs 文件?这将如何将 url 重定向为:~/products.aspx?id=1
  • 我会添加行httpContext.Response.StatusCode = 301;。我还必须删除代码以在末尾添加斜杠,因为它会将我的 URL 参数从 ?foo=bar 分解为 ?foo=bar/,然后我的 Action 会抛出错误,因为 bar/foo 的值无效。
  • 有一篇博客文章使用此代码(或非常相似)更彻底地解释了用法 - eworldui.net/blog/post/2008/04/…
猜你喜欢
  • 2011-01-14
  • 2016-09-22
  • 1970-01-01
  • 2014-12-23
  • 1970-01-01
  • 1970-01-01
  • 2017-02-09
  • 2011-12-14
  • 2014-01-12
相关资源
最近更新 更多