0% found this document useful (0 votes)
21 views2 pages

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

1. The document describes how to implement URL rewriting in ASP.NET using the Global.asax file. 2. It involves adding code to the Global.asax file's Application_BeginRequest method to check the requested URL, retrieve a content ID from the database based on the URL, and redirect to a ShowContents page along with the content ID as a query string parameter while maintaining the original URL in the browser address bar. 3. The ShowContents page is then responsible for displaying the appropriate content based on the passed content ID parameter.

Uploaded by

prnprithvi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

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

1. The document describes how to implement URL rewriting in ASP.NET using the Global.asax file. 2. It involves adding code to the Global.asax file's Application_BeginRequest method to check the requested URL, retrieve a content ID from the database based on the URL, and redirect to a ShowContents page along with the content ID as a query string parameter while maintaining the original URL in the browser address bar. 3. The ShowContents page is then responsible for displaying the appropriate content based on the passed content ID parameter.

Uploaded by

prnprithvi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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