2011-01-12 5 views
5

Il nostro vecchio sito ASP.net ha memorizzato immagini statiche in una sottodirectory nella radice denominata /images.Percorsi di reindirizzamento permanente per file statici in ASP.Net MVC

Il nostro nuovo sito ASP.net MVC memorizza queste immagini nel nuovo layout di /content/Immagini

ho cambiato tutte le pagine del sito per far fronte alle nuove struttura delle cartelle, ma io' mi piace impostare i reindirizzamenti permanenti dalle vecchie immagini statiche alla nuova posizione.

Il nostro sito è ospitato e non ho il controllo su IIS, quindi qual è l'approccio migliore per risolvere questo problema?

+1

Si scopre che il mio host web ha farmi avere controllo su IIS, e quindi sono stato in grado di utilizzare il modulo Url Rewriting per soddisfare le mie esigenze. Tuttavia, lascerò questa domanda nel caso in cui la comunità fornisca una risposta per coloro che si trovano nella situazione in cui pensavo di trovarmi. –

risposta

6

Io uso il seguente codice per i miei MVC 2 siti:

// 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(); 
    } 
} 

Poi ho semplicemente aggiungere route legacy alla mia mappa del percorso:

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

+1 Come usi questo codice? Dove crei il file .cs per LegacyHandler, MVC3? Come funzionerà per reindirizzare url come: ~/products.aspx? Id = 1 – Picflight

+0

Aggiungerei la riga 'httpContext.Response.StatusCode = 301;'. Ho anche ** dovuto rimuovere il codice per aggiungere una barra alla fine ** perché stava rompendo i miei parametri URL da '? Foo = bar' a'? Foo = bar/', e quindi la mia azione avrebbe generato un errore perché 'bar /' non è un valore valido per 'foo'. – ANeves

+0

c'è un articolo del blog con questo codice (o molto simile) che spiega l'uso più approfonditamente - http://www.eworldui.net/blog/post/2008/04/aspnet-mvc---legacy-url-routing.aspx –