【问题标题】:Is it possible to add routes other that Application_start Event in asp.net web forms是否可以在 asp.net Web 表单中添加 Application_start 事件以外的路由
【发布时间】:2013-09-20 10:42:33
【问题描述】:

我有我的客户创建的动态菜单。我已经在global.asaxApplication_Start 事件中注册了我的路线。现在我想在我的客户添加新菜单时注册路线。我怎样才能做到这一点?

目前我已经在 global.asax 中的 Application_Start 事件中注册了所有旧路由。

void Application_Start(object sender, EventArgs e)
{
    RegisterRoutes(RouteTable.Routes);
}

void RegisterRoutes(RouteCollection routes)
{

    string connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlConnection sqlCon = new SqlConnection(connectionstring);
    SqlCommand sqlCmd = new SqlCommand("Sch_Sp_GetRegisterRoutes", sqlCon);
    sqlCmd.CommandType = CommandType.StoredProcedure;

    SqlDataAdapter sda = new SqlDataAdapter(sqlCmd);
    DataTable dt = new DataTable();

    try
    {
        sda.Fill(dt);
    }
    catch (Exception ex)
    {
    }
    finally
    {
        sqlCon.Close();

    }
    if (dt != null && dt.Rows.Count > 0)
    {
        foreach (DataRow dr in dt.Rows)
        {
            try
            {
                routes.MapPageRoute(dr["Menuname"].ToString().Replace(" ",string.Empty),
              dr["URL"].ToString(),
               "~/" + dr["Pagename"].ToString());
            }
            catch (Exception exx)
            {

            }

        }
    }


}

在管理员登录中,我为我的客户提供了类似 cms 的功能,以便他们可以创建新页面。现在我的问题是,如何在路由中注册这些新的页面路由?。我不想每次客户添加新页面时都重新启动我的应用程序。我只是希望当客户添加新页面时我调用一个方法来在我的路由中注册这些页面。我怎样才能做到这一点?

终于有办法解决了。 我有调用以下方法来重新启动我的应用程序..

 System.Web.HttpRuntime.UnloadAppDomain();

【问题讨论】:

  • @dav_i 先生,我使用过 Asp.net 网络表单,请给我这个方法的解决方案..
  • appologies,我没有看到您标题的网络表单部分。你已经尝试过什么?
  • @dav_i 我没有在我的项目中使用 mvc 请指导我获取 asp.net 网络表单
  • 你尝试过做什么?请注意,您的问题已被搁置 [on-hold]:请阅读 meta.stackexchange.com/questions/156810/…

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


【解决方案1】:

现在我已使用以下代码重新启动我的应用程序,通过这些新创建的 url 在路由中注册。

 System.Web.HttpRuntime.UnloadAppDomain();

【讨论】:

  • 我将代码添加到我的 RouteConfig 文件以从数据库中提取并将那里定义的路由添加到 Routes 表(使用 routes.MapPageRoute)。然后在创建路由的管理页面中,我包含了一个运行 System.Web.HttpRuntime.UnloadAppDomain() 方法的链接,该方法再次调用 RegisterRoutes 函数。像魅力一样工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-03
  • 2012-08-20
  • 1970-01-01
  • 2021-12-01
  • 2015-10-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多