2011-01-12 3 views
5

이전 ASP.net 사이트는 /images이라는 루트의 하위 디렉토리에 정적 이미지를 저장했습니다. 나는이 사이트의 모든 페이지를 변경했습니다ASP.Net MVC의 정적 파일에 대한 영구 리디렉션 레거시 경로

우리의 새로운 ASP.net MVC 사이트 /내용의 새로운 레이아웃에서 이러한 이미지를 저장/이미지는 새 폴더 구조에 대처하기 위해,하지만 난 ' 오래된 고정 이미지에서 새 위치로 영구 리디렉션을 설정하고 싶습니다.

우리 사이트는 호스팅되어 있으며 IIS에 대한 제어권이 없으므로이를 해결하는 가장 좋은 방법은 무엇입니까?

+1

그것은 내 웹 호스트가 나를 보자 않았다고 밝혀 IIS를 제어하므로 URL 재 작성 모듈을 사용하여 요구 사항을 충족시킬 수있었습니다. 그러나이 질문을 남겨두고 커뮤니티가 내가 생각한 상황에있는 사람들에게 대답을 제공 할 것입니다. –

답변

6

내 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())); 
+0

+1이 코드를 어떻게 사용합니까? 어디에서 LegacyHandler, MVC3 용 .cs 파일을 만드시겠습니까? ~/products.aspx? id = 1 – Picflight

+0

'httpContext.Response.StatusCode = 301;'줄을 추가 할 것입니다. 나는 또한'? foo = bar'에서 '? foo = bar /'로 URL 매개 변수를 깨고 있었기 때문에 ** 끝에 슬래시를 추가하는 코드를 삭제해야했습니다. 그러면 내 Action은 오류를 던질 것입니다. 'bar /'는'foo'에 유효한 값이 아닙니다. – ANeves

+0

이 코드가 포함 된 블로그 기사 (또는 이와 유사한 내용)가 사용법을보다 철저하게 설명합니다. - http://www.eworldui.net/blog/post/2008/04/aspnet-mvc---legacy-url-routing.aspx –

관련 문제