First We Have To Add "Global - Asax" File Into Our Application. Collapse

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

1. First we have to add "Global.asax" file into our application.

 Collapse
// Write this method prototype like this,

void Application_BeginRequest(object sender, EventArgs e)


// write structure of code like this...

// First get the url of the request,

string absoluteUrl = Request.Url.AbsolutePath.ToString();


//then search for the specific page into that url of request,(means you can

//rewrite the url for some speific pages only.

if (absoluteUrl.Contains("/test/default.aspx"))
{

string pageid = absoluteUrl.Substring


(absoluteUrl.LastIndexOf('/') + 1).Trim();
//now skip first 5 characters because they are "MyPage".

pageid = pageid.Substring(6); //this will give "33".

// now generate the original Url which is required by our application.

string path = "~/test/default.aspx?mypageid=" + pageid;


//now redirect to the target page,

Response.Redirect(path);

// STEP 1 (ShowContents.aspx)

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// This page displays contents from a database for a ContentID. This file

// must contain the following line in it's Page_Load() event.

Context.RewritePath(Path.GetFileName(Request.RawUrl)); //needs System.IO

// STEP 2 (Global.asax)

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// Following code goes in your Global.asax file, in the root directory of

// the project. This file captures the incoming requests' URL, gets the
// Content ID of the content related to the URL and redirects the user to

// ShowContents.aspx?ContentID=XX. While the user still sees the requested

// URL in the address bar.

<%@ Import Namespace="System.IO" %>


protected void Application_BeginRequest(object sender, EventArgs e)
{
// If the requested file exists

if (File.Exists(Request.PhysicalPath))
{
// Do nothing here, just serve the file

}
// If the file does not exist then

else if (!File.Exists(Request.PhysicalPath))
{
// Get the URL requested by the user

string sRequestedURL = Request.Path.Replace(".aspx", "");

// You can retrieve the ID of the content from database that is

// relevant to this requested URL (as per your business logic)

int nId = 0;
////// nId = GetContentIDByPath(sRequestedURL); \\\\\

// The ShowContents.aspx page should show contents relevant to

// the ID that is passed here

string sTargetURL = "~/ShowContents.aspx?ContentID=" + nId.ToString();

// Owing to RewritePath, the user will see requested URL in the

// address bar

// The second argument should be false, to keep your references

// to images, css files

Context.RewritePath(sTargetURL, false);
}
}
// That's all! :-)

You might also like