简体   繁体   English

asp.net webforms中的动态路由

[英]Dynamic Routing in asp.net webforms

Can we add dynamically routes to global.asax file我们可以动态添加路由到global.asax文件吗

Suppose if I have multiple routes for the same page for example假设我在同一页面上有多条路线,例如

While my actual URL for the page is like http://website.com/en/about-us .虽然我的页面实际 URL 类似于http://website.com/en/about-us

My question now is: is there a way I can dynamically define these routes in global.asax file in such a way that it reads the URL entered by users like http://website.com/about and then compares it with database table and redirects it to the correct page which is http://website.com/en/about-us ?我现在的问题是:有没有一种方法可以在global.asax文件中动态定义这些路由,以便它读取用户输入的 URL,例如http://website.com/about ,然后将其与数据库表进行比较,然后将其重定向到正确的页面,即http://website.com/en/about-us

Taking into consideration following Table Structure:考虑到以下表结构:

Id  URL_Name    URL                                 Actual_URL                              Page_Handler
1   Home        http://website.com/                 http://website.com/                     Default.aspx
2   About Us    http://website.com/about            http://website.com/en/about-us          About.aspx
3   About Us    http://website.com/about-us         http://website.com/en/about-us          About.aspx
4   About Us    http://website.com/en/about         http://website.com/en/about-us          About.aspx
5   Contact     http://website.com/contact          http://website.com/en/contact-us        Contact.aspx
6   Contact     http://website.com/en/contact       http://website.com/en/contact-us        Contact.aspx

Right now I have to configure each route manually in the global.asax :现在我必须在global.asax手动配置每个路由:

        if(HttpContext.Current.Request.Url.ToString().ToLower().Equals("http://website.com/about")
        {
            HttpContext.Current.Response.Status = "301 Moved Permanently";
            HttpContext.Current.Response.Redirect("http://website.com/en/about-us");
        }


        if(HttpContext.Current.Request.Url.ToString().ToLower().Equals("http://website.com/en/about")
        {
            HttpContext.Current.Response.Status = "301 Moved Permanently";
            HttpContext.Current.Response.Redirect("http://website.com/en/about-us");
        }

A pointer to a good example or a solution is highly appreciated.高度赞赏指向一个好的例子或解决方案的指针。

I find routes in global.asax are great for static resources especially if you want a nice, sexy extensionless URLs for SEO.我发现 global.asax 中的路由非常适合静态资源,特别是如果你想要一个漂亮、性感的 SEO 无扩展 URL。

For dynamic pages/URLs though, I tend to have a catch all route that handles the request if it doesn't match any static routes.但是,对于动态页面/URL,如果请求不匹配任何静态路由,我倾向于使用捕获所有路由来处理请求。

eg例如

    // ignore
    routes.Add(new System.Web.Routing.Route("{resource}.axd/{*pathInfo}", new System.Web.Routing.StopRoutingHandler()));

    // sexy static routes
    routes.MapPageRoute("some-page", "some-sexy-url", "~/some/rubbish/path/page.aspx", true);

    // catch all route
    routes.MapPageRoute(
       "All Pages",
       "{*RequestedPage}",
       "~/AssemblerPage.aspx",
       true,
       new System.Web.Routing.RouteValueDictionary { { "RequestedPage", "home" } }
    );

So, when a request comes in it checks each static route in turn and executes the specified page.因此,当请求进来时,它会依次检查每个静态路由并执行指定的页面。 If no match is found, it drops through to the catch all and then AssemblerPage.aspx handles the request.如果没有找到匹配项,它会进入 catch all,然后 AssemblerPage.aspx 处理请求。 This AssemblerPage will analyse the requested URL and redirect, rewrite path or stick some controls on the page to render - basically, it can do whatever you want it to do.这个 AssemblerPage 将分析请求的 URL 并重定向、重写路径或在页面上粘贴一些控件来呈现 - 基本上,它可以做任何你想做的事情。

In your case, I'd have the AssemblerPage check the DB and compare the requested URL with the URLs in your table.在您的情况下,我会让 AssemblerPage 检查数据库并将请求的 URL 与表中的 URL 进行比较。 Then simply redirect or rewrite path.然后只需重定向或重写路径。

Would ASP.NET Routing help you here ? ASP.NET Routing 会在这里帮助你吗?

Have a look at this: - ASP.net URL rewrite based off query string ID看看这个: - ASP.net URL rewrite based off query string ID

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

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