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.aspxBy FriendlyUrls this will be
www.yupcode.com/home.aspx?customer=123
www.yupcode.com/homeInstall it via NuGet Package
www.yupcode.com/home/customer/123
OR by
Tools > Library Package Manager > Manage Nuget Packages for Solution
After Installation completed you find following component automatically added in your application
Global.asax
<%@ Application Language="C#" %> <%@ Import Namespace="System.Web.Routing" %> void Application_Start(object sender, EventArgs e) { RouteConfig.RegisterRoutes(RouteTable.Routes); }
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
To get Query string data in IList<>
You can also pass Friendly URL using <a> tag , hyperlink and link button which are mostly used in asp .net
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) { IListtemp = 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 codeThis 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