Click here to Skip to main content
15,906,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created an event in global.asax file, to redirect to the url , I succeeded but I dont know why every time I write the short form of url and I redirect to the page the styles and icons get confused. For example the icon of the page has not loaded! But when I use the full form url everything is okay.

What I have tried:

<pre> void Application_BeginRequest(Object sender, EventArgs e)
    {
        string originalPath = HttpContext.Current.Request.Path.ToLower();
        if (originalPath.Contains("/employment"))
        {
            originalPath = "~/WebSites/Employment/Employment.aspx";
           
            Context.RewritePath(originalPath,"/employment",string.Empty,false);
           
            
        }
Posted
Updated 27-Mar-18 1:12am
Comments
Afzaal Ahmad Zeeshan 27-Mar-18 4:37am    
What is the short form of the URL?
F-ES Sitecore 27-Mar-18 7:18am    
You're probably using relative paths to your css files etc in the html. Use something like Page.ResolveUrl to generate the urls rather than hard-coding relative paths.
maysam_p82 27-Mar-18 8:56am    
can u show me an example of it?

1 solution

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:
C#
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[^]
 
Share this answer
 
Comments
maysam_p82 27-Mar-18 9:44am    
thank you very much, but I didnt understand what did your method does?:(
and if I was going that the method catch when I write "/employment" every where at the url then what should I do?
I want to this work when the user types www.website.com/employment
Richard Deeming 27-Mar-18 9:46am    
My method changes the URL back to the original URL just before the page renders. This should fix the relative URLs in your output.

But as I said, it would be better to use the URL Routing, which is much more robust.
maysam_p82 28-Mar-18 0:36am    
yess it worked , thank you so much for your help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900