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

跟風遠走 提交于 2020-01-01 09:18:53

问题


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?


回答1:


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" } });
}



回答2:


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



来源:https://stackoverflow.com/questions/4938028/how-to-use-routing-asp-net-4-webforms-with-query-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!