简体   繁体   English

ASP.NET Web表单中的ASP.Net路由

[英]ASP.Net Routing in asp.net web-forms

I am using ASP.Net web form Routing for my website but i want to make it more structure & hide all the Querystring ID's with proper Structure like Language/Category/PageName-Title example: www.abc.com/en/sports/cricket-world-cup 我正在为我的网站使用ASP.Net Web表单路由,但我想使其更具结构性并使用适当的结构(如Language/Category/PageName-Title示例)隐藏所有Querystring ID,例如: www.abc.com/en/sports/cricket-world-cup

but with my current routing technique i am able to make it work as 但是使用我目前的路由技术,我可以使其工作

www.abc.com/en/1/13/cricket-world-cup

domain/english-folder/language-id/page-id/page-title

How can i make it more structured as `www.abc.com/en/sports/cricket-world-cup 我如何使其结构更像`www.abc.com/en/sports/cricket-world-cup

Since i have few Query-string i designed it this way 由于我查询字符串很少,所以我以此方式设计

    //For Page.aspx
    routes.MapPageRoute("Page_Route", "en/page/{LID}/{PID}/{PageName}", "~/en/Page.aspx", false,
                new RouteValueDictionary {
                    { "LID", "0"},
                    { "PID", "0" },
                    { "PageName", "Page-not-found" }},
                new RouteValueDictionary {   
                    { "LID", "[0-9]{1,8}" },
                    { "PID", "[0-9]{1,8}" },
                });

Result of above Routing get me Friendly URL like this 上面的路由结果使我得到这样的友好URL
www.abc.com/en/1/13/cricket-world-cup

But i want to further structure the URL like the one in example. 但是我想像示例中那样进一步构造URL。

www.abc.com/en/sports/cricket-world-cup

Example: This url i found is more structure/ http://www.thenational.ae/news/world/europe/turkey-providing-jobs-a-future-for-more-greeks 范例:我发现这个网址更具结构性/ http://www.thenational.ae/news/world/europe/turkey-providing-jobs-a-future-for-more-greeks

How can i implement the same structure are they passing querystrings as hiddenfields. 我如何实现相同的结构,因为它们将querystrings作为hiddenfields传递。 Please suggest best approach for such url. 请为此类网址建议最佳方法。

another example is http://mashreqbank.com/uae/en/corporate/lending/trade-finance.aspx 另一个示例是http://mashreqbank.com/uae/en/corporate/lending/trade-finance.aspx

they are using asp.net but i am not sure if it is ASP.Net MVC or ASP.Net webform. 他们正在使用asp.net,但我不确定它是ASP.Net MVC还是ASP.Net Webform。

I have been struggling with this kind of routing for quite long and even i cant find a complete example which can take into consideration more than one query-string as most of the example are based on one query-string. 我一直在努力进行这种路由,甚至我找不到一个完整的示例,该示例可以考虑多个查询字符串,因为大多数示例都基于一个查询字符串。

Any help from coding gurus with example would be highly appreciated. 我们非常感谢您通过示例对专家进行编码。

UPDATE: Let us take this Table into consideration which i use to store page name, title, handler, language regions etc.. This is just a demo table & doesnt resemble the actual website which i am refering to for sample structured url. 更新:让我们考虑一下该表,我用它来存储页面名称,标题,处理程序,语言区域等。这只是一个演示表,与我所引用的示例结构化URL的实际网站并不相似。 I am not even sure if they are going it using URL routing or these are actual physical folder & files like old style website where you create actual folder and place files in their etc.. 我什至不知道他们是否要使用URL路由,或者它们是实际的物理文件夹和文件,例如旧样式的网站,您可以在其中创建实际的文件夹并将文件放置在它们中。

Page_ID     Page_Name               Page_Title              Page_Handler        Parent_Page_ID  Language_ID     Region_ID
1           Home                    Home                    index.aspx              0               1               uae
2           Personal                Personal                index.aspx              0               1               uae
3           Accounts & Deposits     Accounts & Deposits     index.aspx              2               1               uae
4           Current Account         Current Account         current-account.aspx    3               1               uae
5           Current Gold Accounts   gold Account            gold-account.aspx       3               1               uae
6           Easy Saver              Easy Saver Account      saver-account.aspx      3               1               uae
7           Fixed Deposits          Fixed Account           fixed-account.aspx      3               1               uae
8           Loans                   Loans                   index.aspx              2               1               uae
9           Personal Loans          Personal Loans          index.aspx              8               1               uae
10          car Loans               car Loans               car-loan.aspx           8               1               uae

website example for above structure http://mashreqbank.com/ 以上结构的网站示例http://mashreqbank.com/

We can use a common page handler if the page design is same this is what i am assuming let us say page.aspx , 如果页面设计相同,我们可以使用通用页面处理程序,这就是我假设让我们说的page.aspx

Pleae feel to make changes to structure and data inorder to achieve the desired result. Pleae感觉要对结构和数据进行更改,以便获得所需的结果。

You could simply have something like: 您可以简单地拥有以下内容:

routes.MapPageRoute(
    "article-route",
    "en/{category}/{pageName}",
    "~/en/page.aspx");

And then on en/page.aspx you can access the category and pageName, assuming you have a query to find the correct article based on those two variables: 然后在en/page.aspx您可以访问category和pageName,假设您有一个查询来基于这两个变量查找正确的文章:

string category = Page.RouteData.Values["category"] as string;
string pageName = Page.RouteData.Values["pageName"] as string;

Though, if you can't identify a page simply by category and pageName (which seems true per your updated question), you may need the id in the route. 但是,如果您不能仅通过categorypageName来识别页面(对于您的更新问题来说似乎是正确的),则可能需要该路线中的id In this case, you can ignore the category and pageName route values as they are only there for a nice-looking URL. 在这种情况下,您可以忽略categorypageName路由值,因为它们仅用于美观的URL。 The only parameter we care about is the id since that is how you identify an article. 我们关心的唯一参数是id因为这是您识别文章的方式。 For example, here are three examples of various routes 例如,这是各种路线的三个示例

routes.MapPageRoute(
    "uae-route",
    "uae/en/{category}/{id}/{pageName}",
    "~/en/index.aspx");

routes.MapPageRoute(
    "gbr-route",
    "gbr/en/{category}/{id}/{pageName}",
    "~/en/index.aspx");

routes.MapPageRoute(
    "account-route",
    "en/{id}/{pageName}",
    "~/en/current-account.aspx");

//then on en/index.aspx and current-account.aspx
int pageId= 0;
if (Int32.TryParse(Page.RouteData.Values["id"] as string, out pageId)) {
    // get the page details (including parent page id and language id if needed)
    // where Page_ID=pageId. 
}

From the sounds of it though, you want to do the first example above, meaning you'll need a way to pull articles by simply category and pageName and not any type of id . 从声音pageName ,您想做上面的第一个示例,这意味着您将需要一种仅通过categorypageName而不是任何类型的id提取文章的方法。

UPDATE: You don't need to create a route for each path... that's the whole point of routing. 更新:您不需要为每个路径创建一个路由...这就是路由的重点。 If you have multiple handlers, then you'll at least need a path for each handler ( .aspx page). 如果您有多个处理程序,则每个处理程序至少需要一个路径( .aspx页)。

It'd be easiest to use an id in the URL, because according to your data structure, that is the only way to identify the page you want to pull. 在URL中使用id最简单,因为根据您的数据结构,这是识别要提取的页面的唯一方法。 So let's use your example, using these routes: 因此,让我们使用您的示例,使用以下路线:

www.abc.com/en/personal www.abc.com/en/personal

www.abc.com/en/personal/acounts-deposits/ www.abc.com/en/personal/acounts-deposits/

www.abc.com/en/personal/acounts-deposits/current-account www.abc.com/en/personal/acounts-deposits/current-account

www.abc.com/en/personal/acounts-deposits/current-gold-account www.abc.com/en/personal/acounts-deposits/current-gold-account

www.abc.com/en/personal/acounts-deposits/easy-saver www.abc.com/en/personal/acounts-deposits/easy-saver

You have one route: 您有一条路线:

routes.MapPageRoute(
    "personal_route",
    "en/personal/{category}/{pageName}",
    "~/personal.aspx",
    false,
    new RouteValueDictionary { { "category", string.Empty }, {"pageName", string.Empty}}); //allow for no values

And the personal.aspx has the following code: 而且personal.aspx具有以下代码:

protected void Page_Load(object sender, EventArgs e)
{
    string category = Page.RouteData.Values["category"] as string;
    string pageName = Page.RouteData.Values["pageName"] as string;
    if (String.IsNullOrEmpty(category)) {
        //no category... must be the /personal route. handle that    
    }
    else if (String.IsNullOrEmpty(pageName))  {
        //no page name, just load the category page content
        //Psuedo query against your non-existant data schema
        //SELECT * FROM SiteContent WHERE Page_Handler='Personal' AND Page_Category=category && Page_URL=""
    }
    else {
        //SELECT * FROM SiteContent WHERE Page_Handler='Personal' AND Page_Category=category && Page_URL=pageName
    }
}

As you can see, your problem is you have no way to identify pages without the IDs. 如您所见,您的问题是您无法识别没有ID的页面。 You'd need to replace all those IDs in your table with unique URLs. 您需要用唯一的URL替换表中的所有ID。 It's doable. 这是可行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM