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

查看:19
本文介绍了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 Web 表单具有完全不同的视图.后面的代码类似,但一个访问类别表/过程和另一个内容.

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

And then make sure the URLs have either Category or Content in the path. You still get the catch-all with *queryvalues

如果您有以下 uri http://www.example.com/Content/Press,您可以使用以下内容访问 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的开头不包含静态CategoryContent,MapRouter将始终满足第一个映射(Category映射)并且永远不知道跳过它.

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天全站免登陆