The problem is relative URLs. The browser thinks it's looking at
/employment
, but the page thinks it's looking at
/WebSites/Employement/Employement.aspx
, so it generates URLs relative to that path. Because they're at different depths, the relative URLs generated on the server won't work on the client.
You typically need to rewrite the URL back to the original before the page renders. Something like this should work:
private static readonly object OriginalUrlKey = new object();
void Application_BeginRequest(Object sender, EventArgs e)
{
string originalUrl = Context.Request.RawUrl;
if (originalUrl.IndexOf("/employment", StringComparison.OrdinalIgnoreCase) != -1)
{
context.Items[OriginalUrlKey] = originalUrl;
string newPath = "~/WebSites/Employment/Employment.aspx";
Context.RewritePath(newPath, false);
}
}
void Application_PostMapRequestHandler(object sender, EventArgs e)
{
var originalUrl = Context.Items[OriginalUrlKey] as string;
if (originalUrl == null) return;
var page = Context.Handler as Page;
if (page != null)
{
page.PreRenderComplete += Page_PreRenderComplete;
}
else
{
RestoreUrl(originalUrl);
}
}
void Page_PreRenderComplete(object sender, EventArgs e)
{
var originalUrl = Context.Items[OriginalUrlKey] as string;
if (originalUrl != null) RestoreUrl(originalUrl);
}
void RestoreUrl(string originalUrl)
{
int index = originalUrl.IndexOf('?');
if (index != -1) originalUrl = originalUrl.Remove(index);
Context.RewritePath(originalUrl, false);
Context.Items.Remove(OriginalUrlKey);
}
A better option would be to use the built-in routing options:
URL Routing | Microsoft Docs[
^]