Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Routing ASP.NET 4 WebForms with Query String?

First, this is not MVC, WebForms only..

I'm using routing to keep my site backwards compatible for our clients, while make my project organized.

I'm also thinking of moving our encrypted query string to a more friendly url. How this works is our clients have to bookmark a huge encrypted url to prevent them from guessing our other clients by changing an id around.

But instead of having this huge url, wanted to add a route like LoginClientName.aspx for each client and have the encrypted query string hard coded or maybe in database.

But don't see a way to add a query to the MapPageRoute..

Was thinking of something like this (know it doesnt work)

routes.MapPageRoute("MapClient1", "LoginClient1.aspx", "Login.aspx?secure=mylongquerystring");
routes.MapPageRoute("MapClient2", "LoginClient2.aspx", "Login.aspx?secure=differentmylongquerystring");

Now this throws exception since it doesn't allow a ? in url.. any ideas how to accomplish this? or is it impossible?

like image 632
jaekie Avatar asked Feb 08 '11 20:02

jaekie


2 Answers

take a look at this:
http://msdn.microsoft.com/en-us/library/cc668177.aspx

basically what its saying is:

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


and then:

void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("",
        "SalesReport/{locale}/{year}/{*queryvalues}", "~/sales.aspx");

    routes.MapPageRoute("SalesSummaryRoute",
        "SalesReportSummary/{locale}", "~/sales.aspx");

    routes.MapPageRoute("SalesDetailRoute",
        "SalesReportDetail/{locale}/{year}/{*queryvalues}", "~/sales.aspx",
        false);

...

    routes.MapPageRoute("ExpenseDetailRoute",
        "ExpenseReportDetail/{locale}/{year}/{*queryvalues}", "~/expenses.aspx",
        false,
        new RouteValueDictionary 
            { { "locale", "US" }, { "year", DateTime.Now.Year.ToString() } },
        new RouteValueDictionary 
            { { "locale", "[a-z]{2}" }, { "year", @"\d{4}" } },
        new RouteValueDictionary 
            { { "account", "1234" }, { "subaccount", "5678" } });
}
like image 52
Letseatlunch Avatar answered Sep 21 '22 02:09

Letseatlunch


Does this mean that you'd have to specify every route individually for each client? (if Yes, you could have always used web.config urlMapping for this)

Instead, use the client name as part of the route and then use the client name to look up your reallylongquerystring

something like this:

routes.MapPageRoute("ClientLoginRoute","Login/{clientName}","~/forms/login.aspx")

and then on the login.aspx page access the client name etc and look up the long string

String reallyLongQueryString = Magic.GetReallyLongQueryString(Page.RouteData.Values["clientName"]);

Dim reallyLongQueryString as String = Magic.GetReallyLongQueryString(Page.RouteData.Values("clientName"))

I'm presuming here that it doesn't matter if a client knew the name of another client as they wouldn't know the login details (if that makes sense)...as they would still need to enter the credentials etc

like image 37
davidsleeps Avatar answered Sep 18 '22 02:09

davidsleeps