如何使用带有查询字符串的路由ASP.NET 4 WebForms?

时间:2022-05-05 08:09:34

First, this is not MVC, WebForms only..

首先,这不是MVC,只是WebForms ..

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.

我也在考虑将加密的查询字符串移动到更友好的URL。这是如何工作的,我们的客户必须为一个巨大的加密网址添加书签,以防止他们通过更改ID来猜测我们的其他客户端。

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.

但是,不是拥有这个巨大的网址,而是想为每个客户端添加类似LoginClientName.aspx的路由,并将加密的查询字符串硬编码或者可能在数据库中。

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

但是没有看到向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?

现在这引发了异常,因为它不允许一个?在网址..任何想法如何实现这一目标?还是不可能?

2 个解决方案

#1


6  

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

basically what its saying is:

看看这个:http://msdn.microsoft.com/en-us/library/cc668177.aspx基本上是这样说的:

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


3  

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)

这是否意味着您必须为每个客户单独指定每条路线? (如果是,您可以一直使用web.config urlMapping)

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

而是使用客户端名称作为路由的一部分,然后使用客户端名称来查找真正的longquerystring

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

然后在login.aspx页面*问客户端名称等,并查找长字符串

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

我在这里假设一个客户端知道另一个客户端的名称并不重要,因为他们不知道登录详细信息(如果这是有道理的)......因为他们仍然需要输入凭据等

#1


6  

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

basically what its saying is:

看看这个:http://msdn.microsoft.com/en-us/library/cc668177.aspx基本上是这样说的:

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


3  

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)

这是否意味着您必须为每个客户单独指定每条路线? (如果是,您可以一直使用web.config urlMapping)

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

而是使用客户端名称作为路由的一部分,然后使用客户端名称来查找真正的longquerystring

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

然后在login.aspx页面*问客户端名称等,并查找长字符串

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

我在这里假设一个客户端知道另一个客户端的名称并不重要,因为他们不知道登录详细信息(如果这是有道理的)......因为他们仍然需要输入凭据等