Thursday, June 18, 2015

Excluding folders upon publishing the website

In VS2013, after you have setup the "publish" (right click on the website and choose Publish Web Site), a new configuration file (website.publishproj) will be added to the project.

To exclude the folders, you have to open this file and add the following section with the "project" section:

  <ItemGroup>
    <ExcludeFromPackageFolders Include="log;temp;">
      <FromTarget>Remove temp folders</FromTarget>
    </ExcludeFromPackageFolders>
  </ItemGroup>

In the "Include" attribute, it contains the folder to be removed when publishing the website. The above example excluding "log" and "temp" folders.


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;

    }