Learning
Learning

Reputation: 20001

asp.net webforms routing not working properly

Assuming my hierarchical urls or friendly url's are as show below and some of the pages has different page handler due to different design or forms associated with them

www.abc.com/personal
www.abc.com/personal/acounts-deposits
www.abc.com/personal/acounts-deposits/current-account
www.abc.com/personal/acounts-deposits/current-gold-account
www.abc.com/personal/acounts-deposits/easy-saver-ac
www.abc.com/personal/acounts-deposits/fixed-account
www.abc.com/loans
www.abc.com/loans/personal-loans
www.abc.com/loans/car-loans

And my Database structure with sample data is as show below

Page_ID     Page_Name               Page_url                                    Page_Handler        Parent_Page_ID
1           Home                    home                                        index.aspx              0               
2           Personal                personal                                    Personal.aspx           0               
3           Accounts & Deposits     personal/acounts-deposit                    Personal.aspx           2               
4           Current Account         personal/acounts-deposit/current-account    Personal.aspx           3               
5           Current Gold Accounts   personal/acounts-deposit/gold-account       gold-account.aspx       3               
6           Easy Saver              personal/acounts-deposit/easy-saaver-ac     Personal.aspx           3               
7           Fixed Deposits          personal/acounts-deposit/fixed-account      fixed-account.aspx      3               
8           Loans                   loans                                       loans.aspx              0               
9           Personal Loans          loans/personal-loans                        loans.aspx              8               
10          car Loans               car-loans                                   car-loan.aspx           8     

In my Menu_Table I can identify page by Page_url as it is unique.

    routes.MapPageRoute("Personal_Route", "en/{*path}", "~/en/personal.aspx", false,
        new RouteValueDictionary {
                    { "path", "Page-not-found" },{ "PageName", "Page-not-found" }
                });


    routes.MapPageRoute("Personal_Route2", "en/{*path}", "~/en/gold-account.aspx", false,
        new RouteValueDictionary {
                    { "path", "Page-not-found" },{ "PageName", "Page-not-found" }
                });

    routes.MapPageRoute("Personal_Route2", "en/{*path}", "~/en/fixed-account.aspx", false,
        new RouteValueDictionary {
                    { "path", "Page-not-found" },{ "PageName", "Page-not-found" }
                });

Above route code always uses first route personal.aspx as handler for all url by default

I have tried for quite some time but i am not able to get it right, I would appreciate help in this regard so that each page is handled by it correct page handler based on Page_url

I want page to be handler by there page handler as show below

www.abc.com/personal  ----  (Personal.aspx)
www.abc.com/personal/acounts-deposits -----(Personal.aspx)
www.abc.com/personal/acounts-deposits/current-account -----(Personal.aspx)
www.abc.com/personal/acounts-deposits/current-gold-account ----- (gold-account.aspx)
www.abc.com/personal/acounts-deposits/easy-saver-ac -----(Personal.aspx)
www.abc.com/personal/acounts-deposits/fixed-account ----- (fixed-account.aspx(
www.abc.com/loans ----- (loans.aspx) 
www.abc.com/loans/personal-loans ----- (loans.aspx)
www.abc.com/loans/car-loans ----- (car-loan.aspx)

Upvotes: 3

Views: 2831

Answers (3)

Ravindra Kumar
Ravindra Kumar

Reputation: 601

You should be more specific while while catching request in your routing table. In your current routing table your are catching all the routes in all of your rules. That`s why the first route is always satisfying every requested url.

I think you must remove * before the path.

You can refer to following url for more clarification:

http://www.prideparrot.com/blog/archive/2012/7/understanding_routing#intro

Upvotes: 1

Richa Jain
Richa Jain

Reputation: 641

My VS2010 solution contains several projects: web, business layer classes, data access layer classes, etc. My web project was called "SystemName.WebForms". The period in the web project name interferes with ASP.NET 4.0's WebForm's routing for some strange reason. Once I renamed my project to "SystemName_WebForms" all of the routes work correctly.

WITH A PERIOD IN THE WEB PROJECT NAME:

only "scenario2" and "scenario4" work WITHOUT A PERIOD IN THE WEB PROJECT NAME:

all scenarios work ROUTES:

RouteTable.Routes.MapPageRoute("scenario1", "scenario1/{option1}", "~/About.aspx");
RouteTable.Routes.MapPageRoute("scenario2", "scenario2.aspx", "~/About.aspx");
RouteTable.Routes.MapPageRoute("scenario3", "scenario3", "~/About.aspx");
RouteTable.Routes.MapPageRoute("scenario4", "scenario4.xxx", "~/About.aspx");

Also refer this link: http://msdn.microsoft.com/en-us/library/vstudio/dd329551(v=vs.100).aspx

Upvotes: 1

makemoney2010
makemoney2010

Reputation: 1242

I'm not sure your approach is the right one.Let me explain:

In your case you map your rout with same configuration

  routes.MapPageRoute("Personal_Route", "en/{*path}"
  routes.MapPageRoute("Personal_Route2", "en/{*path}"

and so on.

Each of those routes will be absolutly matched to all requests to your application

To achive your goal is better to map in this way :

 //Take care to the sequensce of routes becuase if you will use route 
 //"personal" before route "account" and you will receive a request to
 //"http://domain.com/personal/somethinghere route account will match 
 //"en/personl/ and will proceed to redirect to "personal" destination 
 routes.MapPageRoute("account", "en/personal/{q}/" ...........
 routes.MapPageRoute("personal", "en/personal" ............


 //Same as above 
 routes.MapPageRoute("loancar", "en/loan/{q}" .........
 routes.MapPageRoute("loan", "en/loan"

Now when you have correctly create and choosed your route you can access to your parameres in a simple manner and choose which is the right action for particular situation in example:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.RouteData.Values.ContainsKey("q"))
        {
            if (Page.RouteData.Values["q"] !=null)
            {
                string p = (string)Page.RouteData.Values["q"];
                switch (p)
                {
                    case "a":
                        //do something
                    case "b":
                    //do something
                    case "c":
                        // Do Something
                        break;
                    case "d":
                        // Do Something
                        break;
                    default:
                        // Do Something
                        break;
                }
            }
        }
    }

Now the you have to make some little change to your database where you will fill it only with the last page of your url.

Take care that is really important how you mapping it ..... I have shown you a simple example with just "static route"(personal) and dynamic route with only one parameter called path. There are lot of manner to achieve your goal with a much better granularity all depend from your needs.I hope it is helpfull for you.

UPDATE: You don't have to create all route, you need to create only the top level route you need then you need to create or new page for each section or using multiwiex controller to split a page in multiple section which take care to show right content to the customer in example if you have personal page which will be used to show 2/3/4 section of your page you may use a multiview controll which depend from the {q} parameter and then use the switch case like here :

 switch (p)
                {
                    case "a":
                        //do something
                        multiview.ActiveViewIndex=0
                    case "b":
                    //do something
                        multiview.ActiveViewIndex=1
                    case "c":
                        // Do Something
                         multiview.ActiveViewIndex=2
                        break;
                    case "d":
                        // Do Something
                          multiview.ActiveViewIndex=3
                        break;
                    default:
                         multiview.ActiveViewIndex=4
                        break;
                }

It is the simple approach.

In this case in example route "PersonalRoute" which expose pattern personal/acounts-deposit could be use for this particolar case and if you need to redirect to a particolar situatiom.Now the question is that i don't know how is your business logic and i don't know your dal configurations so i cannot get you an exactly way to figure out.

I don't knwo how many task will be perfomed or run within your page so i cannot give you more info about it.

Create a unique route is not a good approach or idea.

Upvotes: 5

Related Questions