Riz
Riz

Reputation: 7012

Routes handling in ASP.NET

I am working in ASP.NET WebForms and C#. I am trying to add routes for different pages. This is sample code from my global.asax which registers Routes

protected void Application_Start(object sender, EventArgs e)
{
    RegisterRoutes(RouteTable.Routes);
    //AppSettings = AppConfig.AppSettings.Settings;
    //ConSettings = AppConfig.ConnectionStrings.ConnectionStrings;
}

public static void RegisterRoutes(RouteCollection routes)
{
    routes.Add("Admin_Users_Update", new Route("Admin/Users/Update/{UserId}", new RoutingHandler("~/Forms/Admin/Users/UpdateUser.aspx")));
}

It works fine. And if we open url like /Admin/Users/Update/1 it opens the edit form nicely. But problem starts if don't follow the pattern or make any change. Like if we enter

/Admin/Users/Update/1/2

or

/Admin/Users/Update/

it will simply show a 404 page.

Do you know any way how can we handle it? So that if there is little difference in url pattern, we should be able to still handle that.

Upvotes: 1

Views: 495

Answers (1)

coder net
coder net

Reputation: 3475

Well, if you specify a pattern and ask the route handler to look for it, it is only going to look for that pattern. The framework follows your rules.

Your options are

1) find out all the different possible patterns and register the routes (you can probably use some id for /1/2 etc).

2) You can specify regular expressions for routing rules. Look here.

3) check out the open source url rewriter . it may give you what you want.. sample here.

4) skip the routing and use your own http module that sits in the pipeline listening for requests and handle it yourself. You can probably still leverage the built in routing by reading the route handler section and applying it. I am not sure but worth a try.

Upvotes: 1

Related Questions