Custom VirtualPathProvider

   1: using System;
   2: using System.Web;
   3: using System.Web.Util;
   4: using System.Web.Hosting;
   5:  
   6: public class SimpleVPP : VirtualPathProvider {
   7:     public static void AppInitialize() {
   8:         HostingEnvironment.RegisterVirtualPathProvider(new SimpleVPP());
   9:     }
  10:  
  11:     public override string CombineVirtualPaths(string basePath, string relativePath) {
  12:  
  13:         // If the path is relative, let normal processing happen
  14:         if (!VirtualPathUtility.IsAbsolute(relativePath))
  15:             return base.CombineVirtualPaths(basePath, relativePath);
  16:         
  17:         // Determine the pseudo site from the request.  To demonstrate, we just get it from the
  18:         // query string, but it could come from other places, like the http host header
  19:         string site = HttpContext.Current.Request.QueryString["site"];
  20:  
  21:         // If we couldn't, default to normal processing
  22:         if (site == null)
  23:             return base.CombineVirtualPaths(basePath, relativePath);
  24:  
  25:         // Make it app relative (i.e. ~/...)
  26:         relativePath = VirtualPathUtility.ToAppRelative(relativePath);
  27:  
  28:         // Remap the virtual path to be inside the correct pseudo site
  29:         return "~/PseudoSites/" + site + relativePath.Substring(1);
  30:     }
  31: } 

 

application_start或httpModule里注册再用, 也不是特好用, 但总比其他折腾来的方便多了...

还是Routing好...

posted @ 2009-01-27 16:08  new 维生素C.net()  阅读(441)  评论(1编辑  收藏  举报