Hello Friend ! Today I am writing about URL rewriting in ASP.NET, which is very useful concept for dynamic application. For SEO point of view URL rewriting is very useful.
The RewritePath(String) methodology redirects a call for participation for a resource to a special path than the one that's indicated by the requested computer address. If you've got to reset the virtual path in order that requests from the shopper for server resources resolve properly, use the overload of this methodology that takes the rebase ClientPath parameter and set the parameter to false.
URL revising is helpful after you need to reconstitute the pages in your net application, and you wish to form certain that individuals UN agency have bookmarked previous URLs will still use them when you've got stirred pages. computer address revising allows you to transparently forward requests to the new page location.
If you wish to change a web site to use URLs that ar additional easy and ar optimized for search engines, a additional strong different is to use ASP.NET routing. For additional info, see ASP.NET Routing.
The following example shows the way to use the RewritePath methodology to change an online web site to retort to URLs that don't mirror the file structure within the computing device. the primary block of code is Associate in Nursing ASP.NET web content that's named RewritePath.aspx. It needs a question string. If the name of your web site is WebSite1, the computer address http://localhost/WebSite1/RewritePath.aspx?page=1 displays "Page 1" within the browser. The block of code that follows the online page is that the Application_BeginRequest event handler within the world.asax file. This code intercepts requests for URLs like http://localhost/WebSite1/page1 and converts them to the shape that's needed for RewritePath.aspx before they're processed. Therefore, the computer address http://localhost/WebSite1/page1 invokes RewritePath.aspx with the query-string parameter that displays "Page 1" within the browser. If a computer address like http://localhost/WebSite1/page1 is received, Associate in Nursing overload of RewritePath is invoked that allows you to produce a price for the PathInfo property further as a question string parameter.
EXAMPLE:
SQL Function:
CREATE FUNCTION [dbo].[RemoveSpecialChars] (@TextInput VARCHAR(2000)) RETURNS VARCHAR(2000)
WITH SCHEMABINDING
BEGIN
IF @TextInput IS NULL
RETURN NULL
DECLARE @TextOutPut VARCHAR(2000) SET @TextOutPut = ''
DECLARE @l INT SET @l = LEN(@TextInput)
DECLARE @p INT SET @p = 1
WHILE @p <= @l
BEGIN
DECLARE @c INT SET @c = ASCII(SUBSTRING(@TextInput, @p, 1))
IF @c BETWEEN 48 AND 57 OR @c BETWEEN 65 AND 90 OR @c BETWEEN 97 AND 122
BEGIN
SET @TextOutPut = @TextOutPut + CHAR(@c)
END
ELSE
BEGIN
SET @TextOutPut = @TextOutPut + '-'
END
SET @p = @p + 1
END
SET @TextOutPut=REPLACE(@TextOutPut,'--','-')
SET @TextOutPut=REPLACE(@TextOutPut,'---','-')
SET @TextOutPut=REPLACE(@TextOutPut,'----','-')
SET @TextOutPut=REPLACE(@TextOutPut,' ','-')
IF(SUBSTRING(@TextOutPut,LEN(@TextOutPut),1)='-')
SET @TextOutPut=SUBSTRING(@TextOutPut,1,(LEN(@TextOutPut)-1))
RETURN @TextOutPut
END
aspx Page:
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { Label1.Text = "Page=" + Request.QueryString["page"] + " PathInfo=" + Request.PathInfo; } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html>
Global.asax
void Application_BeginRequest(Object sender, EventArgs e) { string originalPath = HttpContext.Current.Request.Path.ToLower(); if (originalPath.Contains("/page1")) { Context.RewritePath(originalPath.Replace("/page1", "/RewritePath.aspx?page=page1")); } if (originalPath.Contains("/page2")) { Context.RewritePath(originalPath.Replace("/page2", "/RewritePath.aspx"), "pathinfo", "page=page2"); } }
I hope you will like this article.







0 comments:
Post a Comment