Thursday, May 22, 2014

ASP.NET URL Routing


 For SEO purposes it might be better to do URL routing for public facing asp.net applications. This will make the URLs more readable and parameters can be separated by “/” instead of “?” and “&”.

If you are using ASP.NET 4.0 and IIS 7 or higher it is easy to do URL routing in asp.net application. You have to do only 2 steps to setup URL routing

       1) Create a function to register routes in global.asax

void RegisterRoutes(RouteCollection routes)
        {
            routes.Ignore("{resource}.aspx/{*pathInfo}");

            routes.MapPageRoute(
               "",      // Route name
               "Home",      // Route URL
               "~/Default.aspx" // Web page to handle route
            );
        }
2) Invoke the function from Application_Start method

 void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            RegisterRoutes(RouteTable.Routes);
        }

Now any request to http://webiste/Home will be redirected to default.aspx page

Now how to pass parameters? You can add parameters to the route URL as given below

routes.MapPageRoute(
               "About",      // Route name
               "AboutTest/{controller}/{action}/{id}"// Route URL
               "~/About.aspx", // Web page to handle route
            true,
               new RouteValueDictionary { { "controller", "food" }, { "action", "show" },{"id","myid"} });

The parameters can be accessed from the page using “Page.RouteData.Values”

if (Page.RouteData.Values["controller"] != null)
            {
                Response.Write(Page.RouteData.Values["controller"]);
            }
            if (Page.RouteData.Values["action"] != null)
            {
                Response.Write(Page.RouteData.Values["action"]);
            }


Wednesday, May 21, 2014

C# Sort generic list by column name


Data in DataTable can be sorted using string column name

DataView dv = new DataView(ds.Tables[0], "","SortExpression" + " " + "SortDirection", DataViewRowState.CurrentRows);

For list we cannot pass sort expression and direction in string format. We have to find the column using reflection and then sort the column as given below

PropertyInfo pi = typeof(Employee).GetProperty("[ColumnName]");
lstData.OrderBy(i => pi.GetValue(i, null)).ToList();


Given below is an example in ASP.NET
-------------------------------------------------------------
HTML (Add inside ‘form’ tags)
    <asp:DropDownList runat="server" ID="ddlSortColumn"></asp:DropDownList>
    <asp:DropDownList runat="server" ID="ddlSortDirection">
        <asp:ListItem Text="asc"></asp:ListItem>
        <asp:ListItem Text="desc"></asp:ListItem>
    </asp:DropDownList>
    <asp:Button runat="server" ID="btnReport" Text="Report"
        onclick="btnReport_Click" />
    <br />
    <asp:GridView runat="server" ID="grvData"></asp:GridView>

C# (CodeBehind)
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IList<PropertyInfo> props = new List<PropertyInfo>(typeof(Employee).GetProperties());
                ddlSortColumn.DataSource = props;
                ddlSortColumn.DataTextField = "Name";
                ddlSortColumn.DataValueField = "Name";
                ddlSortColumn.DataBind();

                grvData.DataSource = GetData();
                grvData.DataBind();
            }
        }
        protected void btnReport_Click(object sender, EventArgs e)
        {
            PropertyInfo pi = typeof(Employee).GetProperty(ddlSortColumn.SelectedValue);
            if (ddlSortDirection.SelectedItem.Text == "asc")
            {
                grvData.DataSource = GetData().OrderBy(i => pi.GetValue(i, null)).ToList();
            }
            else
            {
                grvData.DataSource = GetData().OrderByDescending(i => pi.GetValue(i, null)).ToList();
            }
            grvData.DataBind();
        }
        public List<Employee> GetData()
        {
            List<Employee> employees = new List<Employee>();
            employees.Add(new Employee() { Id = 1, Name = "Employee 1", Age = 25 });
            employees.Add(new Employee() { Id = 2, Name = "Employee 2", Age = 35 });
            employees.Add(new Employee() { Id = 3, Name = "Employee 3", Age = 39 });
            return employees;
        }
        public class Employee
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }