Sunday, June 14, 2015

Asp.net WebForm + Routing

To add the routing support in the WebForm, you need to add "System.Web.Routing" reference to your website.

Then, in the global.asax file, add the following codes:

void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        RegisterRoute(System.Web.Routing.RouteTable.Routes);
    }
   
    void RegisterRoute(System.Web.Routing.RouteCollection r)
    {
        // shows all customer
        r.MapPageRoute("allcust",
                        "customer",
                        "~/customer.aspx");
       
        // shows 1 customer profile
        r.MapPageRoute("cust",
                        "customer/{code}",
                        "~/customer.aspx");

        // shows the doc for the customer
        r.MapPageRoute("cust_doc",
                        "customer/{code}/{doc_no}",
                        "~/customer.aspx");

        // this allows any sub-level of parameters and the page requires to parse 'Page.RouteData.Values["queryvalues"]' (only 1 value).
        // The caller can call this route by the following url:
        //      /cust
        //      /cust/a001
        //      /cust/a001/inv123456
        //
        r.MapPageRoute("cust_query",
                        "cust/{*queryvalues}",
                        "~/customer.aspx");
       
    }

Finally, add all the aspx page that you need to display the information. In our example, we have only one page that is customer.aspx. You can get the parameter values in "Page.RouteData". Note: the last route that has been setup in the above was not shown in the following codes.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.RouteData.Values.Count == 0)
        {
            this.lbl1.Text = "cust list";
        }
        else
        {
            if (this.RouteData.Values.Count == 1)
            {
                this.lbl1.Text = "customer => " + this.RouteData.Values["code"];
            }
            else
            {
                this.lbl1.Text = "customer => " + this.RouteData.Values["code"]
                    +",doc_no=" + this.RouteData.Values["doc_no"];
            }

            foreach (var item in this.RouteData.Values.Keys)
            {
                System.Diagnostics.Debug.WriteLine("param=" + item
                     + ",value=" + this.RouteData.Values[item]);
            }
        }
    }

If you need to set the navigate URL in the hyperlink control, this can be done easily as shown below:

    protected void Page_Load(object sender, EventArgs e)
    {
        // manually setup the URL based on the parameters.
        this.all_cust_link.NavigateUrl = RouteTable.Routes.GetVirtualPath(null, "allcust", null).VirtualPath;

        RouteValueDictionary d;
        d = new RouteValueDictionary();
        d.Add("code", "a001");

        this.one_cust_link.NavigateUrl = RouteTable.Routes.GetVirtualPath(null, "cust", d).VirtualPath;

        d = new RouteValueDictionary();
        d.Add("code", "a001");
        d.Add("doc_no", "inv123456");
        this.one_doc_link.NavigateUrl = RouteTable.Routes.GetVirtualPath(null, "cust_doc", d).VirtualPath;

    }


No comments:

Post a Comment