English 中文(简体)
如何将路由ASP.NET 4 WebForms与查询字符串一起使用?
原标题:How to use Routing ASP.NET 4 WebForms with Query String?

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

我使用路由来保持我的网站向后兼容我们的客户,同时使我的项目有条理。

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,不如为每个客户端添加一个类似LoginClientName.aspx的路由,并对加密的查询字符串进行硬编码,或者可能在数据库中。

但是看不到向MapPageRoute添加查询的方法。。

正在考虑这样的事情(知道它不起作用)

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

现在这抛出异常,因为它不允许?在url中。。有什么想法可以做到这一点吗?还是不可能?

最佳回答

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" } });
}
问题回答

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

相反,使用客户端名称作为路由的一部分,然后使用客户端名称查找reallylongquerystring

像这样的东西:

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

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

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

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

我在这里假设,如果一个客户知道另一个客户的名字并不重要,因为他们不会知道登录详细信息(如果这有意义的话)。。。因为他们仍然需要输入凭据等





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签