最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > asp教程

WebForms使用System.Web.Routing

日期:07-23    来源:中国设计秀    作者:cnwebshow.com

老赵同学写过 在Web应用程序开发过程中利用asp.net MVC框架的实战技巧 ,Routing现在可以作为URLRewriting技术的替代者,出现在asp.net mvc框架中,将它应用于WebForms上也是很简单的,可以到codeplex上下载ASP.NET MVC WebFormRouting Demo 。xiP中国设计秀
xiP中国设计秀
实现的原理也是很简单的:xiP中国设计秀
xiP中国设计秀
1、创建一个自定义的实例化你的页面的 IRouteHandler   xiP中国设计秀
xiP中国设计秀
     1: public class WebFormRouteHandler : IRouteHandler {xiP中国设计秀
xiP中国设计秀
   2:  xiP中国设计秀
        publicxiP中国设计秀
WebFormRouteHandler(stringxiP中国设计秀
virtualPath)   3:  xiP中国设计秀
            : thisxiP中国设计秀
(virtualPath, truexiP中国设计秀
) {   4:  xiP中国设计秀
        }   5:  xiP中国设计秀
    6:  xiP中国设计秀
        publicxiP中国设计秀
WebFormRouteHandler(stringxiP中国设计秀
virtualPath, boolxiP中国设计秀
checkPhysicalUrlaccess) {   7:  xiP中国设计秀
            ifxiP中国设计秀
(virtualPath == nullxiP中国设计秀
) {   8:  xiP中国设计秀
                throwxiP中国设计秀
newxiP中国设计秀
ArgumentNullException("virtualPath"xiP中国设计秀
);   9:  xiP中国设计秀
            }  10:  xiP中国设计秀
   11:  xiP中国设计秀
            ifxiP中国设计秀
(!virtualPath.StartsWith("~/"xiP中国设计秀
)) {  12:  xiP中国设计秀
                throwxiP中国设计秀
newxiP中国设计秀
ArgumentException("virtualPath must start with a tilde slash: "~/""xiP中国设计秀
, "virtualPath"xiP中国设计秀
);  13:  xiP中国设计秀
            }  14:  xiP中国设计秀
   15:  xiP中国设计秀
            thisxiP中国设计秀
.VirtualPath = virtualPath;  16:  xiP中国设计秀
            thisxiP中国设计秀
.CheckPhysicalUrlAccess = checkPhysicalUrlAccess;  17:  xiP中国设计秀
        }  18:  xiP中国设计秀
   19:  xiP中国设计秀
        /// <summary>xiP中国设计秀
  20:  xiP中国设计秀
        /// This is the full virtual path (using tilde syntax) to the WebForm page.xiP中国设计秀
  21:  xiP中国设计秀
        /// </summary>xiP中国设计秀
  22:  xiP中国设计秀
        /// <remarks>xiP中国设计秀
  23:  xiP中国设计秀
        /// Needs to be thread safe so this is only settable via ctor.xiP中国设计秀
  24:  xiP中国设计秀
        /// </remarks>xiP中国设计秀
  25:  xiP中国设计秀
        publicxiP中国设计秀
stringxiP中国设计秀
VirtualPath { get; PRivatexiP中国设计秀
set; }  26:  xiP中国设计秀
   27:  xiP中国设计秀
        /// <summary>xiP中国设计秀
  28:  xiP中国设计秀
        /// Because we're not actually rewriting the URL, ASP.NET's URL Auth will apply xiP中国设计秀
  29:  xiP中国设计秀
        /// to the incoming request URL and not the URL of the physical WebForm page.xiP中国设计秀
  30:  xiP中国设计秀
        /// Setting this to true (default) will apply URL access rules against the xiP中国设计秀
  31:  xiP中国设计秀
        /// physical file.xiP中国设计秀
  32:  xiP中国设计秀
        /// </summary>xiP中国设计秀
  33:  xiP中国设计秀
        /// <value>True by default</value>xiP中国设计秀
  34:  xiP中国设计秀
        publicxiP中国设计秀
boolxiP中国设计秀
CheckPhysicalUrlAccess { get; set; }  35:  xiP中国设计秀
   36:  xiP中国设计秀
        publicxiP中国设计秀
IHttpHandler GetHttpHandler(RequestContext requestContext) {  37:  xiP中国设计秀
            stringxiP中国设计秀
virtualPath = GetSubstitutedVirtualPath(requestContext);  38:  xiP中国设计秀
            ifxiP中国设计秀
(thisxiP中国设计秀
.CheckPhysicalUrlAccess && !UrlAuthorizationModule.CheckUrlAccessForPrincipal(virtualPath, requestContext.HttpContext.User, requestContext.HttpContext.Request.HttpMethod))  39:  xiP中国设计秀
                throwxiP中国设计秀
newxiP中国设计秀
SecurityException();  40:  xiP中国设计秀
   41:  xiP中国设计秀
            var page = BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeofxiP中国设计秀
(Page)) asxiP中国设计秀
IHttpHandler;  42:  xiP中国设计秀
            ifxiP中国设计秀
(page != nullxiP中国设计秀
) {  43:  xiP中国设计秀
                //Pages that don't implement IRoutablePage won't have the RequestContextxiP中国设计秀
  44:  xiP中国设计秀
                //available to them. Can't generate outgoing routing URLs without that context.xiP中国设计秀
  45:  xiP中国设计秀
                var routablePage = page asxiP中国设计秀
IRoutablePage;  46:  xiP中国设计秀
                ifxiP中国设计秀
(routablePage != nullxiP中国设计秀
)  47:  xiP中国设计秀
                    routablePage.RequestContext = requestContext;  48:  xiP中国设计秀
            }  49:  xiP中国设计秀
            returnxiP中国设计秀
page;  50:  xiP中国设计秀
        }  51:  xiP中国设计秀
   52:  xiP中国设计秀
        /// <summary>xiP中国设计秀
  53:  xiP中国设计秀
        /// Gets the virtual path to the resource after applying substitutions based on route data.xiP中国设计秀
  54:  xiP中国设计秀
        /// </summary>xiP中国设计秀
  55:  xiP中国设计秀
        /// <param name="requestContext"></param>xiP中国设计秀
  56:  xiP中国设计秀
        /// <returns></returns>xiP中国设计秀
  57:  xiP中国设计秀
        publicxiP中国设计秀
stringxiP中国设计秀
GetSubstitutedVirtualPath(RequestContext requestContext) {  58:  xiP中国设计秀
            ifxiP中国设计秀
(!VirtualPath.Contains("{"xiP中国设计秀
))  59:  xiP中国设计秀
                returnxiP中国设计秀
VirtualPath;  60:  xiP中国设计秀
   61:  xiP中国设计秀
            //Trim off ~/xiP中国设计秀
  62:  xiP中国设计秀
            stringxiP中国设计秀
virtualPath = VirtualPath.Substring(2);  63:  xiP中国设计秀
   64:  xiP中国设计秀
            Route route = newxiP中国设计秀
Route(virtualPath, thisxiP中国设计秀
);  65:  xiP中国设计秀
            VirtualPathData vpd = route.GetVirtualPath(requestContext, requestContext.RouteData.Values);  66:  xiP中国设计秀
            ifxiP中国设计秀
(vpd == nullxiP中国设计秀
)  67:  xiP中国设计秀
                returnxiP中国设计秀
VirtualPath;  68:  xiP中国设计秀
            returnxiP中国设计秀
"~/"xiP中国设计秀
+ vpd.VirtualPath;  69:  xiP中国设计秀
        }  70:  xiP中国设计秀
    }2、使用自定义的 IRouteHandler注册一个新的RoutesxiP中国设计秀
xiP中国设计秀
   1:  xiP中国设计秀
publicxiP中国设计秀
classxiP中国设计秀
Global : System.Web.Httpapplication   2:  xiP中国设计秀
    {   3:  xiP中国设计秀
    4:  xiP中国设计秀
        protectedxiP中国设计秀
voidxiP中国设计秀
Application_Start(objectxiP中国设计秀
sender, EventArgs e)   5:  xiP中国设计秀
        {   6:  xiP中国设计秀
            RegisterRoutes(RouteTable.Routes);   7:  xiP中国设计秀
        }   8:  xiP中国设计秀
    9:  xiP中国设计秀
        publicxiP中国设计秀
staticxiP中国设计秀
voidxiP中国设计秀
RegisterRoutes(RouteCollection routes)  10:  xiP中国设计秀
        {  11:  xiP中国设计秀
            //We are intentionally creating this backdoor as a demonstration of xiP中国设计秀
  12:  xiP中国设计秀
            //bad security practices.xiP中国设计秀
  13:  xiP中国设计秀
            routes.MapWebFormRoute("Secret"xiP中国设计秀
, "BackDoor"xiP中国设计秀
, "~/Admin/SecretPage.aspx"xiP中国设计秀
, falsexiP中国设计秀
);  14:  xiP中国设计秀
            routes.MapWebFormRoute("Blocked"xiP中国设计秀
, "FrontDoor"xiP中国设计秀
, "~/Admin/SecretPage.aspx"xiP中国设计秀
, truexiP中国设计秀
);  15:  xiP中国设计秀
   16:  xiP中国设计秀
            //Even though we are not checking physical url access in this route, it should still block because the incoming xiP中国设计秀
  17:  xiP中国设计秀
            //request url would start with /Admin.xiP中国设计秀
  18:  xiP中国设计秀
            routes.MapWebFormRoute("Admin"xiP中国设计秀
, "Admin/{*anything}"xiP中国设计秀
, "~/Admin/SecretPage.aspx"xiP中国设计秀
, falsexiP中国设计秀
);  19:  xiP中国设计秀
   20:  xiP中国设计秀
            routes.MapWebFormRoute("Named"xiP中国设计秀
, "foo/bar"xiP中国设计秀
, "~/forms/blech.aspx"xiP中国设计秀
);  21:  xiP中国设计秀
            routes.MapWebFormRoute("Numbers"xiP中国设计秀
, "one/two/three"xiP中国设计秀
, "~/forms/haha.aspx"xiP中国设计秀
);  22:  xiP中国设计秀
              23:  xiP中国设计秀
            //Maps any requests for /haha/*.aspx to /forms/hahah.aspxxiP中国设计秀
  24:  xiP中国设计秀
            routes.MapWebFormRoute("Substitution"xiP中国设计秀
, "haha/{filename}"xiP中国设计秀
, "~/forms/haha.aspx"xiP中国设计秀
);  25:  xiP中国设计秀
        }  26:  xiP中国设计秀
    }相关文章: xiP中国设计秀
xiP中国设计秀
<span id="ctl00_ArticleTopHeader_ArticleTitle" class="ArticleTopTitle">Fight 404 errors with ASP.NET Routing :<a href="http://www.codeproject.com/KB/aspnet/routing404.aspx">http://www.codeproject.com/KB/aspnet/routing404.aspx</a></span>xiP中国设计秀

本文引用地址:/bc/article_46202.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明