1

Is it possible to Add/Remove routes after application was started?
What I want to do, is to give the ability to manage routes in admin system without the need to restart the web application. So the content team will set routing by their needs.

We use ASP.NET 4 WebForms application.

2 Answers 2

2

It is possible but you will need to write own logic to handle route collection and update based on add/remove to collection.

Code below is just to show basics on how something like this can be achieved. There are many ways to dynamically store and have user update routes.

In Global.asax assume you have some predefined route:

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup

        RegisterRoutes(RouteTable.Routes);

    }

    void RegisterRoutes(RouteCollection routes)
    {
        routes.MapPageRoute("RouteDirect", "about/{user}", "~/about.aspx");
    }

Now About.aspx has a way to read route value.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.RouteData.Values["user"] != null)
        {
            TextBox1.Text = Page.RouteData.Values["user"].ToString();
        }
    }

If you browse to localhost:51604/about/john You will see john in textbox.

Below is simple example on how you can add/remove routes. You can extend on this logic to suit your needs where you can show all existing routes on an secure page for your content team and provide them ability to add/remove/disable route or have more complex way to specify route parameters etc.

        protected void Button1_Click(object sender, EventArgs e)
        {
            //Adding New Route on Button Click
            RouteTable.Routes.MapPageRoute("RouteDirectNew", "aboutnew/{user}", "~/about.aspx");

            //Now route table has 2 routes:
            // about/{user}
            // aboutnew/{user}
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            //Removing new route on button click
            RouteCollection rcollection = new RouteCollection();
            rcollection = RouteTable.Routes;

            Route ToDeleteRoute = null;

            foreach (Route r in rcollection)
            {
                if (r.Url == "aboutnew/{user}")
                    ToDeleteRoute = r;
            }

            if(ToDeleteRoute != null)
                RouteTable.Routes.Remove(ToDeleteRoute);

            //Now route table has only 1 route the original added via global.asax:
            // about/{user}

        }
Sign up to request clarification or add additional context in comments.

Comments

0

According to MSDN, if you want to modify RouteTable.Routes outside Application_start of Global.asax.cs, you need to get Read/Write lock to make sure its thread safty.

So the Button2_Click above should be something like:

    protected void Button2_Click(object sender, EventArgs e)
    {
        //Removing new route on button click
        RouteCollection rcollection = new RouteCollection();

        using (currentRoutes.GetReadLock())
        {
            rcollection = RouteTable.Routes;
        }

        Route ToDeleteRoute = null;

        foreach (Route r in rcollection)
        {
            if (r.Url == "aboutnew/{user}")
                ToDeleteRoute = r;
        }

        if(ToDeleteRoute != null)
        {
            using (currentRoutes.GetWriteLock())
            {
                RouteTable.Routes.Remove(ToDeleteRoute);
            }
        }

        //Now route table has only 1 route the original added via global.asax:
        // about/{user}

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.