ASP.NET 4.0 URL路由 - 类似MapPageRoutes [英] ASP.NET 4.0 URL Routing - Similar MapPageRoutes

查看:392
本文介绍了ASP.NET 4.0 URL路由 - 类似MapPageRoutes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会尽量解释尽我所能。

I will try to explain this the best I can.

我创建了一个CMS,允许你创建分类和内容部分。两者具有完全不同的模板,但我想布线时使用相同的URL路由的MapPageRoute参数。基本上,我需要它来检查别名是一类,如果不打内容部分路由器。

I created a CMS that allows you to create Categories and Content Sections. Both have completely different templates, but I want to use the same URL routing mapPageRoute param when routing. Basically, I need it to check if the alias is a category, if not hit the content section router.

下面是我的Global.asax注册路线:

Here is my Registered Routes on Global.asax:

void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute(
        "Home",
        string.Empty,
        "~/Default.aspx"
    );

    routes.MapPageRoute(
        "Category",
        "{*CategoryAlias}",
        "~/templates/Category.aspx"
    );

    routes.MapPageRoute(
        "Content",
        "{*ContentAlias}",
        "~/templates/Content.aspx"
    );
}

目前,类别做工精细,但是当我把内容部分中的别名的URL它击中的类别和不跳到下一个路线去尝试。该Category.aspx和Content.aspx网络形式有完全不同的看法。在code的背后,是类似但一访问类别表/程序和其他内容。

Currently, Categories work fine, but when I put a content section alias in the URL it hits categories and doesn't skip to the next route to try. The Category.aspx and Content.aspx web forms have completely different views. The code behind is similar but one accesses the Category tables/procedures and the other Content.

如果有人需要更多的信息,只问。

If anyone requires more information just ask.

推荐答案

你有没有尝试过这样的事情?

Have you tried something like this?

void RegisterRoutes(RouteCollection routes) 
{ 
    routes.MapPageRoute( 
        "Home", 
        string.Empty, 
        "~/Default.aspx" 
    ); 

    routes.MapPageRoute( 
        "Category", 
        "Category/{Cat}/{*queryvalues}", 
        "~/templates/Category.aspx" 
    ); 

    routes.MapPageRoute( 
        "Content", 
        "Content/{Cont}{*queryvalues}", 
        "~/templates/Content.aspx" 
    ); 
} 

然后确保的URL路径中有任何一类或内容。您仍然获得包罗万象与 * queryvalues​​

编辑:

如果您有以下URI http://www.example.com/Content/$p$pss 您可以访问 preSS 通过以下内容:

If you have the following uri http://www.example.com/Content/Press you can access Press by using the following:

Page.RouteData.Values["Cont"].ToString();

因此​​,在你Content.aspx页面,获取该字符串,然后用它来确定用户试图去哪个网站。

So, in your Content.aspx page, grab that string and then use that to determine which site the user was trying to get to.

您需要包括某种静态的URL微分从而使MapRouter可以找到在哪里地图的页面。

You need to include some kind of static URL differentiator so that the MapRouter can find where to map the page.

如果您不包括静态类别内容中的URI的开始,MapRouter会总是第一个地图(类别映射)来满足,并永远不知道跳过它。

If you don't include the static Category or Content in the beginning of the uri, the MapRouter will always be satisfied with the first map (the Category mapping) and never know to skip it.

这篇关于ASP.NET 4.0 URL路由 - 类似MapPageRoutes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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