'BlogEngineRes' Undefined

Topics: Business Logic Layer, Controls
Jun 19, 2013 at 3:36 PM
I'm trying to integrate BlogEngine into an existing web site and placed it in a BlogEngine subdirectory. I've got it to the point that the solution will build, but when I try to hit the BlogEngine default.aspx page, I get the following error (and other errors like it if I persist):
Unhandled exception at line 29, column 5 in http://localhost:60047/BlogEngine/Scripts/App/qnotes.js
0x800a1391 - Microsoft JScript runtime error: 'BlogEngineRes' is undefined
After much persistence, I've discovered that BlogEngineRes is supposed to be defined in BlogEngine.Core/Web/HttpHandlers/ResourceHandlers.cs, on line 118. What I haven't discovered is why the code in qnotes.js is trying to call BlogEngineRes before it's been defined, or how it expects to reach the definition for ResourceHandlers.cs at any point in the code.

Even trying to trace the process in the good code of a clean, stand-alone installation of the BlogEngine source code doesn't reach my break-point at line 118 in ResourceHandlers.cs, but I've searched the entire solution for every reference to BlogEngineRes, and found no attempt to define it outside that single line of code.

Can anyone enlighten me on how and when BlogEngineRes is supposed to be defined for the javascript?
Jun 19, 2013 at 5:21 PM
All pages inherit from BlogBasePage so it start there (BlogEngine.Web.Controls.BlogBasePage).
Base page in "OnLoad" method calls header.Render (BlogEngine.Web.Scripting.PageHeader).
In that render method, BE decides what language should be used for resource and puts it in the page header on top, before qnotes.js or anything else.

So now when you check page source, you should see in the page header link to language resource that looks something like:
    <!-- meta and css links here -->
    <script type="text/javascript" src="/blog/en-us.res.axd"></script>
    <script type="text/javascript" src="/blog/Scripts/Auto/01-jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="/blog/Scripts/Auto/02-jquery.cookie.js"></script>
    <!-- more links here -->
This "en-us.res.axd" is what triggers ResourceHandler.cs to be executed. You should have it registered in web.config to work.
Resource handler dynamically builds resource script (for English language in this case) and writes it to the browser with Response.Write(script).

The break point at line 118 only reached when resource not cached yet, you would need to clear browser cache to get there.
Jun 19, 2013 at 7:09 PM
Thanks! The en-us.res.axd file is definitely missing from the problem version, but how do I register it?

When I search for "en-us" in my two BlogEngine installs, the one that's working doesn't come up with a single hit that's not there and identical in the problem version.
Jun 19, 2013 at 7:50 PM
This is the code from PageHeader.cs:
List<LiteralControl> GetScripts()
    var headerScripts = new List<LiteralControl>();
    var tmpl = "\n\t<script type=\"text/javascript\" src=\"{0}\"></script>";
    var lang = BlogSettings.Instance.Language;

    // if specific culture set in blog settings, use it instead
    if (BlogSettings.Instance.Culture.ToLower() != "auto")
        lang = BlogSettings.Instance.Culture;

    var rsrc = HttpHandlers.ResourceHandler.GetScriptPath(new CultureInfo(lang));
    headerScripts.Add(new LiteralControl(string.Format(tmpl, rsrc)));
string.Format(tmpl, rsrc) should produce "<script type="text/javascript" src="/blog/en-us.res.axd"></script>"
and Render() method should inject it to the page header.
Jun 20, 2013 at 2:36 PM
Curiouser and curiouser.

I'm stepping through the code of both versions after clearing the browser cache, arriving at the following code in the Render method of PageHeader.cs file.
            idx = GetIndex(page);
            foreach (var script in GetScripts())
                page.Header.Controls.AddAt(idx, script);
The GetScripts has done its job effectively, each version returning a list of scripts that's identical save that the problem version is returning the location of each script prefixed with the "BlogEngine/" directory, where it successfully located the "Scripts/Auto/" directory to build most of that list. So "<script type="text/javascript" src="/BlogEngine/en-us.res.axd"></script>" is the first script it trying to add to the header there. The working, stand-alone BlogEngine is trying to add "<script type="text/javascript" src="/en-us.res.axd"></script>" at the same point.

So both of them are looking for "en-us.res.axd" at what should functionally be the same place, relative to the BlogEngine installation. But en-us-res.axd never makes it into the ScriptDocuments of the problem version, and the javascript error persists.
Jun 20, 2013 at 3:12 PM
If you go to the browser and look at the page source - do you have "en-us.res.axd" added to the header?
Jun 20, 2013 at 3:32 PM
Yes: "<script type="text/javascript" src="/BlogEngine/en-us.res.axd"></script>" appears in the <head> section of the page source.
Jun 20, 2013 at 4:59 PM
Does it resolve? For example, if you go to "http://rtur.net/blog/en-us.res.axd" you'll actually get the script output to the page.
Script tag looks like: "<script type="text/javascript" src="/blog/en-us.res.axd"></script>"
Does this work in your case?
Jun 20, 2013 at 5:13 PM
No. It doesn't resolve on the problem version.
Jun 20, 2013 at 5:25 PM
Does path look ok, compare to other scripts that resolve? For example, other scripts start with "/" instead of "/BlogEngine"?

If not, you probably have issue with web.config not registering resource handler. It should have this line:
<add verb="*" path="*.res.axd" type="BlogEngine.Core.Web.HttpHandlers.ResourceHandler, BlogEngine.Core" validate="false" />
Jun 20, 2013 at 7:36 PM
That did it! The web.config wasn't registering the resource handler.

Thank you very much.