我需要在Global.asax文件中注册一个动态路由

时间:2015-09-27 12:25:17

标签: c# asp.net webforms routing global-asax

我将页面详细信息存储在菜单表中

MenuName, MenuID, ParentID, PageURL, PageHandler 
Home, 1, 0, /,Default.aspx 
About Us, 2, 0, /about-us/, About.aspx 
Contact, 3, 0, /contact/, contact.aspx 
Mission, 4, 2, /about-us/mission/, About.aspx
Vision, 5, 2, /about-us/vision/, About.aspx

如果我在Global.asax文件中将我的路由编码为静态,那么它可以正常工作,如下图所示。

 public static void RegisterRoutes(RouteCollection routes)
 {
    routes.MapPageRoute("en_Home", "default/", "~/default.aspx", false,
            new RouteValueDictionary {
            { "path", "page-not-found" },{ "pagename", "page-not-found" }
            });

     routes.MapPageRoute("en_aboutUs", "about-us/", "~/about_us.aspx", false,
            new RouteValueDictionary {
            { "path", "page-not-found" },{ "pagename", "page-not-found" }
            });
}

我希望我的路由是动态的,以便我可以从数据库中读取URL和PageHandler值,并将其作为

传递给Global.asax文件中的Route。
public static void RegisterRoutes(RouteCollection routes)
 {          
    string sURL = HttpContext.Current.Request.Url.AbsolutePath;
    //Above Statement give error Say "HttpContext.Current Cant be used in this context

    string PageHandler =  DataProvider.GetPageHandlerByPageURL(sURL);

     routes.MapPageRoute("en_General_Page", sURL, PageHandler, false,
            new RouteValueDictionary {
            { "path", "page-not-found" },{ "pagename", "page-not-found" }
            });     
 }

我没有在此获得任何成功,因为我必须将HttpContext.Current.Request.Url.AbsolutePath值传递给另一个函数,以便我可以获得页面处理程序。它失败并显示错误Cant use HttpCotext in this context

HttpContext.Current.Request.Url.AbsolutePath在路由RegisterRoutes功能中不起作用我必须在Application_BeginRequest中定义它,但它有自己的问题。

如何在我的案例中实现动态路由。我正在使用ASP.Net Webform 4.5

我很感激这方面的帮助。

2 个答案:

答案 0 :(得分:1)

这是ASP.NET欢呼声中的动态路由:)-

public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            RegisterRoute(RouteTable.Routes);
        }

        public void RegisterRoute(RouteCollection routes)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable("T");
            dt.Columns.Add(new DataColumn("Head", typeof(string)));
            dt.Columns.Add(new DataColumn("Desc", typeof(string)));
            dt.Columns.Add(new DataColumn("Url", typeof(string)));

            DataRow dr0 = dt.NewRow();
            dr0["Head"] = "Default";
            dr0["Desc"] = "D/D";
            dr0["Url"] = "~/Default.aspx";
            dt.Rows.Add(dr0);

            DataRow dr1 = dt.NewRow();
            dr1["Head"] = "ERP1";
            dr1["Desc"] = "ERP/ERP1";
            dr1["Url"] = "~/ERP/ERP1.aspx";
            dt.Rows.Add(dr1);

            DataRow dr2 = dt.NewRow();
            dr2["Head"] = "ERP2";
            dr2["Desc"] = "ERP/ERP2";
            dr2["Url"] = "~/ERP/ERP2.aspx";
            dt.Rows.Add(dr2);

            DataRow dr3 = dt.NewRow();
            dr3["Head"] = "W";
            dr3["Desc"] = "W/W";
            dr3["Url"] = "~/WebForm1.aspx";
            dt.Rows.Add(dr3);

            foreach (DataRow d in dt.Rows)
            {
                routes.MapPageRoute(d["Head"].ToString(), d["Desc"].ToString(), d["Url"].ToString());
            }
        }
    }

答案 1 :(得分:0)

你让我困惑;

我很乐意回答这个问题,只需将网址放在网页浏览器中即可。

e.g。

http://localhost/Home

http://localhost/about-us

http://localhost/about-us/mission

http://localhost/about-us/vission

您似乎不了解如何注册路线。

我不跟随;

   MenuName, MenuID, ParentID, PageURL, PageHandler 
    Home, 1, 0, /,Default.aspx 
    About Us, 2, 0, /about-us/, About.aspx 
    Contact, 3, 0, /contact/, contact.aspx 
    Mission, 4, 2, /about-us/mission/, About.aspx
    Vision, 5, 2, /about-us/vision/, About.aspx
当你告诉我我需要的信息时,

我会更新我的答案

<强>更新

似乎我对ASP.Net Webform的理解可能有所不同。 为什么你使用

   routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

而不是......

 routes.MapPageRoute(
     routeName: "SomeRouteName",
     routeUrl: "GuessingHardset",
     physicalFile: "~/about_us.aspx", //also hard set
     checkPhysicalUrlAccess: false
 );

更新

我道歉他们是不同的。

此链接很棒: http://weblogs.asp.net/scottgu/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series

像......那样......

routes.MapPageRoute(
    "en_aboutUs",
    "about-us/{subpage}",
    "~/about_us.aspx",
    false
});

void Page_Load(object sender, EventArgs e)
{
    //Retrive subpage param from "about-us/{subpage}" URL
    string subpage = Page.RouteData.Values["subpage"] as string;
}