Tuesday, 7 January 2014

You Are In

FriendlyURL : The extension less Page URL in ASP .NET

Hello Every one, this is my first post on this blog this is all about the URL rewriting in asp .net. there are old traditional way to do this is asp .net but as of now there are NuGet package available which can do this for you automatically.
 you just need to configure little bit things on your web application. Benificial for SEO and User Friendly URL in asp .net application which is till tough task. but now it is easy to do.
If you page URL is
www.yupcode.com/home.aspx
www.yupcode.com/home.aspx?customer=123
 By FriendlyUrls this will be
 www.yupcode.com/home
www.yupcode.com/home/customer/123
Install it via NuGet Package
  OR by

                 Tools > Library Package Manager > Manage Nuget Packages for Solution
 After Installation completed you find following component automatically added in your application
First of all you need to Put Following code in "Global.asax" file so by that it work on Whole application from start up.

Global.asax

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>

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

Make sure blow code is added in you RouteConfig.cs file

RouteConfig.cs

  public static void RegisterRoutes(RouteCollection routes)
        {
            var settings = new FriendlyUrlSettings();
            settings.AutoRedirectMode = RedirectMode.Permanent;
            routes.EnableFriendlyUrls(settings);
        }

 Run Application and check your address bar

 

You can also pass the query string data from one page to another by simply the following code


C#
  
 protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect(FriendlyUrl.Href("/second","id","123"));
    }
1: "/second" is the page name where you want to redirect
2: "id" & "123" is the query string value which we are passing from one page to another.



Get Query string parameter on second page At page load by Request.GetFriendlyUrlSegments()   whilch is able to get data indidually and in IList<>  Check You below code.
On second.aspx Page insert below code at page load to get data which are passed through the query string and then we can access them in our code

Second.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Routing;
using Microsoft.AspNet.FriendlyUrls; 

public partial class second : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string paramete1 = Request.GetFriendlyUrlSegments()[0].ToString();

        string parameter2 = Request.GetFriendlyUrlSegments()[1].ToString();
    }
}


To get Query string data in IList<>

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Routing;
using Microsoft.AspNet.FriendlyUrls; 

public partial class second : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string paramete1 = Request.GetFriendlyUrlSegments()[0].ToString();

        string parameter2 = Request.GetFriendlyUrlSegments()[1].ToString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        IList temp = Request.GetFriendlyUrlSegments();
        
        foreach (var data in temp)
        { 
           Response.Write(data.ToString()+"");
        }

    }
}


You can also pass Friendly URL using <a> tag , hyperlink and link button which are mostly used in asp .net

using <a> tag

Remeber to Import Name space in Your .aspx Page like below to use this functionality in markup code

This will let you get rid over the aspx extension at URL which is User Friendly and beneficial for SEO too! , Frankly i can say during when i am newbie , I used to see the extension to check whether the website is developed in asp.net by checking extension ha ha . but using some tools your able to check the extension of page :(.
I find this good feature of ASP.NET. Hope you all enjoy it and also like the post. stay tuned for more stuff.



0 comments:

Post a Comment

COMMENT ON THIS OR GIVE ANY SUGGESTIONS & IDEAS