views:

669

answers:

6

I'm using BlogEngine.NET (a fine, fine tool) and I was playing with the TinyMCE editor and noticed that there's a place for me to create a list of external links, but it has to be a javascript file:

external_link_list_url : "example_link_list.js"

this is great, of course, but the list of links I want to use needs to be generated dynamically from the database. This means that I need to create this JS file from the server on page load. Does anyone know of a way to do this? Ideally, I'd like to just overwrite this file each time the editor is accessed.

Thanks!

A: 

If you can't change the file extension (and just return plain text, the caller shouldn't care about the file extension, js is plain text) then you can set up a handler on IIS (assuming it's IIS) to handle javascript files.

See this link - http://msdn.microsoft.com/en-us/library/bb515343.aspx - for how to setup IIS 6 within windows to handle any file extension. Then setup a HttpHandler to receive requests for .js (Just google httphandler and see any number of good tutorials like this one: http://www.devx.com/dotnet/Article/6962/0/page/3 )

marshall
+2  A: 

I would create an HTTPHandler that responds with the desired data read from the db. Just associate the HTTPHandler with the particular filename 'example_link_list.js' in your web-config. Make sure you set

context.Response.ContentType = "text/javascript";

then just context.Response.Write(); your list of external links

JustAsItSounds
A: 

thanks for the response... i don't have access to my IIS. i'm on a shared hosting plan. i was wondering if there's some way to just create a file w/the extension ".js" on it and fill it with text and save it to the server somewhere. is there an easy way to do that, perhaps?

Jason
A: 

Just point it at an aspx file and have that file spit out whatever javascript you need. I did this recently with TinyMCE in PHP and it worked like a charm.

external_link_list_url : "example_link_list.aspx"

In your aspx file:

<%@ Page Language="C#" AutoEventWireup="false" CodeFile="Default.aspx.cs" Inherits="Default" %>
in your code-behind (C#):
using System;

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("var tinyMCELinkList = new Array(");
        // put all of your links here in the right format..
        Response.Write(string.Format("['{0}', '{1}']", "name", "url"));
        Response.Write(");");
    }
}
Wayne
A: 

if your 3rd party code doesn't require that the javascript file has the .js extension, then you can create your HTTPHandler and map it to either .axd or .ashx extension in web.config only - no need to change IIS settings as these extensions are automatically configured by IIS to be handled by asp.net.

<system.web>
  <httpHandlers>
    <add verb="*" path="example_link_list.axd" type= "MyProject.MyTinyMCE, MyAssembly" />
  </httpHandlers>
</system.web>

This instructs IIS to pass all requests for 'example_link_list.axd' (via POST and GET) to the ProcessRequest method of MyProject.MyTinyMCE class in MyAssembly assembly (the name of your .dll)

You could alternatively use Visual Studio's 'Generic Handler' template instead - this will create an .ashx file and code-behind class for you. No need to edit web.config either.

using an HTTPHandler is preferrable to using an .aspx page as .aspx requests have a lot more overheads associated (all of the page events etc.)

JustAsItSounds
A: 

@JustAsItSounds -

Let's say I implement your answer, which sounds good... After I update my Web.Config file, I create a file called "whatever.axd". Then what do I put in it? I'm not familiar w/.axd files...

sorry for being dumb

Jason