2011-10-25 8 views
0

私はthis Routingを働かせました。 Dot Net 4.0 System.Web.Routingです。ルーティングはimages/js/cssなどを削除します

しかし、問題は、ドキュメント内のすべてのパスが機能しなくなったことです。私は、ページが

www.website.com/agents/Agent Name 

そして「本当の」アドレス

www.website.com/portfolio.aspx?aid=123 

の両方として仕事をしたい場合は、私は何をしますか?もちろん、絶対URLを使用して動作させることができます

<img src="http://www.website.com/images/image.png" alt="" /> 

しかし、その方法はありますか?


私は実際に自分の答えが見つかりました:

routes.Add( "AgentFolderGraphicsRoute"、新 ルート( "エージェント/グラフィックス/ {フォルダ}/{ファイル名} {EXT}を"、新しい ImageRouteHandler()));このページの1を少し変更したバージョンです


public class ImageRouteHandler : IRouteHandler 
    { 
     public IHttpHandler GetHttpHandler(RequestContext requestContext) 
     { 
      string folder = requestContext.RouteData.Values["folder"] as string; 
      string filename = requestContext.RouteData.Values["filename"] as string; 
      string ext = requestContext.RouteData.Values["ext"] as string; 

      if (string.IsNullOrEmpty(filename)) 
      { 
       requestContext.HttpContext.Response.Clear(); 
       requestContext.HttpContext.Response.StatusCode = 404; 
       requestContext.HttpContext.Response.End(); 
      } 
      else 
      { 
       requestContext.HttpContext.Response.Clear(); 
       requestContext.HttpContext.Response.ContentType = GetContentType(requestContext.HttpContext.Request.Url.ToString()); 

       // find physical path to image here. 
       string filepath; 
       if (folder != null) filepath = requestContext.HttpContext.Server.MapPath("~/graphics/" + folder + "/" + filename + "." + ext); 
       else filepath = requestContext.HttpContext.Server.MapPath("~/graphics/" + filename + "." + ext); 

       requestContext.HttpContext.Response.WriteFile(filepath); 
       requestContext.HttpContext.Response.End(); 
      } 
      return null; 
     } 

     private static string GetContentType(String path) 
     { 
      switch (System.IO.Path.GetExtension(path)) 
      { 
       case ".bmp": return "Image/bmp"; 
       case ".gif": return "Image/gif"; 
       case ".jpg": return "Image/jpeg"; 
       case ".png": return "Image/png"; 
       default: break; 
      } 
      return ""; 
     } 

http://www.phpvs.net/2009/08/06/aspnet-mvc-how-to-route-to-images-or-other-file-types/

答えて

0

まず最初に確認してくださいあなたのRegisterRoutes()方法がある中:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

リソース(イメージやファイルなど)の要求を無視するようにルーティングエンジンに指示します。

+0

MVC以外のアプリではできないと思います。 – Jesper

+0

MVCは実際にはASP.NET拡張です。 MVCのルーティングエンジンはWebFormsで使用されているものと同じなので、動作するはずです。それを試しましたか? –

+0

System.Web.Routing.RouteCollectionにIgnoreRouteが含まれていないため、できません – Jesper

関連する問題