Absolute URL for all href and src on a page

Topics: Customizing Orchard, Writing modules
Feb 8, 2013 at 8:56 AM
I'm new to Orchard and MVC although I've been programming on c# for a few years. I'm attempting to create a Realtor website that uses a third party MLS connection. For some of their objects I need to provide a "master page" so my website theme can be displayed on their page. For this to work, none of the URL's on the page can be relative they must be absolute.

I started by trying to find each link and append the domain name to it. This quickly failed when I attempted to do so on the layout.cshtml for the style sheet where it ended up referencing <domain>/style.css instead of <domain>/entire_rest_of_theme_path/style.css. I assumed there has to be a way to intercept each all of the links just before rendering the html and making this change so I went hunting for it.

I found one example that says I should write an html helper to accomplish this. I found information on an interface for IWorkContextAccessor that should give me the HttpContext allowing me to gain access to all of the links I assume. However I'm a little clueless as to how to actually accomplish this and what files I'd need to change.

I've read an entire orchard book and numerous online tutorials. I'd love to take a crack at making this a module that when added to a page it shows all the links as absolute. Any nudge in the right direction would be greatly appreciated. I am a newbie so I do appreciate detail.
Feb 10, 2013 at 6:47 AM
Feb 12, 2013 at 7:25 PM
Bertrand - Thank you for the reply and thank you for this module you wrote. Unfortunately I have to admit my understanding of orchard isn't far enough along to implement the custom module I envisioned. But the code already exists in your module, so I simply enabled it. It works great! Although I understand it should be turned off in dev since it appends the actual domain name specified in the dashboard and not a dynamic base URL depending on the environment. Not a problem.

My only issue is that it doesn't affect the entire page, specifically the navigation and header zone. Plus the stylesheet link from the layout.cshtml, I believe is before these two zones. Is there a way I could get all of these links to be absolute?
Feb 12, 2013 at 9:30 PM
PS - I would assume that there may be some "order of events" in the rendering that might prevent what I want to accomplish, for example if the style sheet in the layout is included after the module has already made the links absolute.
Feb 12, 2013 at 9:42 PM
Maybe this is what you should be using then: http://www.w3schools.com/tags/tag_base.asp
Feb 13, 2013 at 5:23 AM
Hmm... I didn't even know that existed. It works like a charm. It is a real pain in the dev environment because every link now takes you to the hardcoded production base path. I added the following to the document.cshtml so that it only effects the few pages that will need behavior and the rest will remain the same. Its a little cheesy because I will have to add another comparison for each page that need absolute paths, but it works. Thanks for everything. I'm thoroughly impressed with Orchard thus far and I've barely scratched the surface.
    public string GetAbsolutePages(string Title)
        string baseTag = string.Empty;
        if (Title == "my page title" || Title == "")
            baseTag = "http://basepathdomain.com";

        return baseTag;   
between the head tag I added:
<base href="@GetAbsolutePages(title)" target="_self" />