开发者

Extends a RouteCollection object for Web Form routing

I use asp.net 4开发者_C百科 and web forms in c#.

I would like to know how to Extends a RouteCollection Class for Web Form routing.

As MSDN state RouteCollectionExtensions Class is avaialbe only for MVC routing. So I would like to know how an equivalent in Web Forms.

Thanks


ASP.NET Routing is available for webforms in .NET 4, and 3.5 I think.

There is guidance on MSDN. http://msdn.microsoft.com/en-us/library/cc668201.aspx

The webforms example requires you to add the following in global.asax

protected void Application_Start(object sender, EventArgs e)
{
    RegisterRoutes(RouteTable.Routes);
}

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("",
       "Category/{action}/{categoryName}",
       "~/categoriespage.aspx");
}

The RouteCollectionExtensions type provides extension methods which extend RouteCollection for the MVC framework, you can create your own extension methods to extend RouteCollection.

Simple example below, i'm sure you will want to put MyRouteCollectionExtensions somewhere else.

using System;
using System.Web.Routing; 

namespace WebFormsExtension 
{
    public class Global : System.Web.HttpApplication
    {

        void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes(RouteTable.Routes);
        }

        public static void RegisterRoutes(RouteCollection routes) 
        {
            routes.MapCustomRoute("SampleRoute/{name}"); 
        }

    }

    public static class MyRouteCollectionExtensions
    {
        public static void MapCustomRoute(this RouteCollection routes, string url)
        {
            PageRouteHandler handler = new PageRouteHandler("~/default.aspx");
            Route myRoute = new Route(url, handler);    
            routes.Add(myRoute);
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜