ASP.Net路由在asp.net web的表单 [英] ASP.Net Routing in asp.net web-forms

查看:116
本文介绍了ASP.Net路由在asp.net web的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.Net Web窗体路由为我的网站,但我想使它更加结构和放大器;隐藏所有的查询字符串的ID与像适当的结构语言/分类/页面名称,标题
例如: www.abc.com/en/sports/cricket-world-cup

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

但我目前的路由技术,我能够让它为

but with my current routing technique i am able to make it work as

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

域/英文文件夹/语言ID /页-ID /页标题

怎样才能使它更有条理为`www.abc.com/en/sports/cricket-world-cup

How can i make it more structured as `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}" },
                });

结果的上方路由让我友好的URL这样的结果
www.abc.com/en/1/13/cricket-world-cup

但我想进一步结构像一个例子中的URL。

But i want to further structure the URL like the one in example.

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

示例:此网址我发现更多的结构/
<$c$c>http://www.thenational.ae/news/world/europe/turkey-providing-jobs-a-future-for-more-greeks

Example: This url i found is more structure/ http://www.thenational.ae/news/world/europe/turkey-providing-jobs-a-future-for-more-greeks

我如何能实现相同的结构,他们通过查询字符串作为hiddenfields。请建议此类网址最好的办法。

How can i implement the same structure are they passing querystrings as hiddenfields. Please suggest best approach for such url.

另一个例子是 http://mashreqbank.com/uae/en /corporate/lending/trade-finance.aspx

他们正在使用asp.net,但我不知道这是否是ASP.Net MVC或ASP.Net Web窗体。

they are using asp.net but i am not sure if it is ASP.Net MVC or 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.

从编码为例大师任何帮助将是非常美联社preciated。

Any help from coding gurus with example would be highly appreciated.

更新:
让我们借此表考虑,我用它来存储页面名称,标题,处理程序,语言的区域等。这只是一个演示台和放大器;犯规类似于实际的网站,我指的样本结构的URL。我什至不知道,如果他们使用URL路由去,或有实际的物理文件夹和放大器;像老风格的网站,你在他们的创建等实际文件夹和文件的地方文件..

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. 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..

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

对于上述结构

网站例如 http://mashreqbank.com/

我们可以使用一个共同的页面处理程序,如果页面设计相同这就是我假设让我们说 page.aspx

We can use a common page handler if the page design is same this is what i am assuming let us say page.aspx,

Pleae感觉来更改结构和数据序以达到期望的结果。

Pleae feel to make changes to structure and data inorder to achieve the desired result.

推荐答案

您可以简单地碰到这样的:

You could simply have something like:

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

,然后在 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:

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

不过,如果你不能简单地通过识别页面类别页面名称(这似乎每次你真更新的问题),您可能需要 ID 的路线。在这种情况下,你可以忽略类别页面名称路由值,因为他们只是有一个好看的网址。我们关心的唯一参数是 ID ,因为这是你如何识别的文章。例如,这里有各种路线的三个例子

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. In this case, you can ignore the category and pageName route values as they are only there for a nice-looking URL. The only parameter we care about is the id since that is how you identify an article. 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. 
}

从它的声音,虽然

,你想要做上面的第一个例子,这意味着你需要一个方式通过简单地拉动文章类别页面名,而不是任何类型的的id

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.

更新::您不必为每个路径的路径...这就是路由的整点。如果你有多个处理器,那么你至少需要为每个处理器(的.aspx 页)的路径。

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).

这将会是最容易使用的 ID 中的URL,因为根据你的数据结构,这是确定你想拉页的唯一途径。因此,让我们用你的榜样,使用这些航线有:

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. 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

您有一个路线:

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

以及 personal.aspx 具有以下code:

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
    }
}

正如你所看到的,你的问题是你有没有办法来确定没有标识的页面。你需要用唯一的网址,以取代所有这些ID在表格中。这是可行的。

As you can see, your problem is you have no way to identify pages without the IDs. You'd need to replace all those IDs in your table with unique URLs. It's doable.

这篇关于ASP.Net路由在asp.net web的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆