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

本文关键字:一个 动态 路由 文件 asax 全局 注册 | 更新日期: 2023-09-27 18:14:55

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

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

如果我将我的路由编码为全局静态。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值,并将其传递给全局路由。Asax文件

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

在这方面我将非常感谢你的帮助。

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

这是ASP中的动态路由。净欢呼:)——

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());
            }
        }
    }

你把我弄糊涂了;

我很乐意回答这个问题,只要把网址,因为他们会出现在web浏览器。

" http://localhost/Home "

" http://localhost/about-us "

" http://localhost/about-us/mission "

" http://localhost/about-us/vission "

你好像不懂如何注册路由。

i do not follow;

   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 -路由与asp -网- 4 - web -形式- vs - 2010和-网- 4 - 0 -系列

之类的…

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