Setting Last-modified http header via xslt Options
Arjandb
Posted: Sunday, March 09, 2008 2:45:38 PM
Rank: Enthusiast

Joined: 8/31/2007
Posts: 32
Location: Utrecht NL
I made a macro which sets the Last-modified http header of Umbraco pages to Umbraco's @updateDate. This caches unmodified pages on the client side, which improves speed and reduces bandwidth on the server. I thought this might be interesting for others, or someone has an even better method?

Below is the basic pinciple. I did make a more advanced version that also takes the updateDate from childnodes, which are displayed as menu items on my pages - so these should also trigger a new Last-modified date. But for clearity, the below example is only for the current page:

Code:

<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
xmlns:umbraco.library="urn:umbraco.library"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:hua="abc:yoursite-com:xslt"
exclude-result-prefixes="msxml umbraco.library abc">
...

<xsl:value-of select="abc:setModif($currentPage/@updateDate)"/>

...



</xsl:stylesheet>
Arjandb
Posted: Sunday, March 09, 2008 2:46:40 PM
Rank: Enthusiast

Joined: 8/31/2007
Posts: 32
Location: Utrecht NL
The following msxsl:script part also belongs to the previeous answer but this fragment raised an error while posting in the forum. Insert this part in de above sxlt.

<msxsl:script language="VB" implements-prefix="abc">
<msxsl:assembly name="System.Web"/>
<!-- assembly reference generates error while saving, so use Skip testing (ignore errors) - not ideal though... -->
<![CDATA[

public Function setModif(dat As String)
Dim mDat As DateTime = Convert.ToDateTime(dat).ToUniversalTime
System.Web.HttpContext.Current.Response.AddHeader("Last-modified", mDat.ToString("r"))
'Return mDat.ToString("r") 'use for debugging
Return ""
End Function

]]>

</msxsl:script>
Arjandb
Posted: Sunday, March 09, 2008 2:47:24 PM
Rank: Enthusiast

Joined: 8/31/2007
Posts: 32
Location: Utrecht NL
The following msxsl:script part also belongs to the previeous answer but this fragment raised an error while posting in the forum. Insert this part in de above xslt while replaceing msxslscript by msxsl:script...

<msxslscript language="VB" implements-prefix="abc">
<msxsl:assembly name="System.Web"/>
<!-- assembly reference generates error while saving, so use Skip testing (ignore errors) - not ideal though... -->
<![CDATA[

public Function setModif(dat As String)
Dim mDat As DateTime = Convert.ToDateTime(dat).ToUniversalTime
System.Web.HttpContext.Current.Response.AddHeader("Last-modified", mDat.ToString("r"))
'Return mDat.ToString("r") 'use for debugging
Return ""
End Function

]]>

</msxslscript>
Arjandb
Posted: Sunday, March 09, 2008 2:47:53 PM
Rank: Enthusiast

Joined: 8/31/2007
Posts: 32
Location: Utrecht NL
The following msxsl:script part also belongs to the previeous answer but this fragment raised an error while posting in the forum. Insert this part in de above xslt while replaceing msxslscript by msxsl:script and the same for msxsl:assambly...

<msxslscript language="VB" implements-prefix="abc">
<msxslassembly name="System.Web"/>
<!-- assembly reference generates error while saving, so use Skip testing (ignore errors) - not ideal though... -->
<![CDATA[

public Function setModif(dat As String)
Dim mDat As DateTime = Convert.ToDateTime(dat).ToUniversalTime
System.Web.HttpContext.Current.Response.AddHeader("Last-modified", mDat.ToString("r"))
'Return mDat.ToString("r") 'use for debugging
Return ""
End Function

]]>

</msxslscript>
Arjandb
Posted: Sunday, March 09, 2008 2:48:31 PM
Rank: Enthusiast

Joined: 8/31/2007
Posts: 32
Location: Utrecht NL
The following msxsl:script part also belongs to the previeous answer but this fragment raised an error while posting in the forum. Insert this part in de above xslt while replaceing msxslscript by msxsl:script and the same for msxsl:assambly...

<msxsl:script language="VB" implements-prefix="abc">
<msxsl:assembly name="System.Web"/>
<!-- assembly reference generates error while saving, so use Skip testing (ignore errors) - not ideal though... -->
<![CDATA[

public Func<!-- remove -->tion setModif(dat As String)
Dim mDat As DateTime = Convert.ToDateTime(dat).ToUniversalTime
System.Web.HttpContext.Current.Response.AddHeader("Last-modified", mDat.ToString("r"))
'Return mDat.ToString("r") 'use for debugging
Return ""
End Function

]]>

</msxsl:script>
Arjandb
Posted: Sunday, March 09, 2008 2:48:47 PM
Rank: Enthusiast

Joined: 8/31/2007
Posts: 32
Location: Utrecht NL
The following msxsl:script part also belongs to the previeous answer but this fragment raised an error while posting in the forum. Insert this part in de above xslt while replaceing msxslscript by msxsl:script and the same for msxsl:assambly...

<msxsl:script language="VB" implements-prefix="abc">
<msxsl:assembly name="System.Web"/>
<!-- assembly reference generates error while saving, so use Skip testing (ignore errors) - not ideal though... -->

public Func<!-- remove -->tion setModif(dat As String)
Dim mDat As DateTime = Convert.ToDateTime(dat).ToUniversalTime
System.Web.HttpContext.Current.Response.AddHeader("Last-modified", mDat.ToString("r"))
'Return mDat.ToString("r") 'use for debugging
Return ""
End Function


</msxsl:script>
Arjandb
Posted: Sunday, March 09, 2008 2:50:17 PM
Rank: Enthusiast

Joined: 8/31/2007
Posts: 32
Location: Utrecht NL
And another try for the msxsl part... teh CDATA around the function raised the error while posting.

Code:
<msxsl:script language="VB" implements-prefix="abc">
<msxsl:assembly name="System.Web"/>
<!-- assembly reference generates error while saving, so use Skip testing (ignore errors) - not ideal though... -->

public Function setModif(dat As String)
  Dim mDat As DateTime = Convert.ToDateTime(dat).ToUniversalTime
  System.Web.HttpContext.Current.Response.AddHeader("Last-modified", mDat.ToString("r"))
  'Return mDat.ToString("r") 'use for debugging
  Return ""
End Function


</msxsl:script>
drobar
Posted: Sunday, March 09, 2008 6:22:15 PM

Rank: Umbracoholic

Joined: 9/8/2006
Posts: 1,285
Location: KY, USA
Why not set it in your Template directly, using the <?ASPNET_HEAD>...</?ASPNET_HEAD> syntax?

cheers,
doug.

MVP 2007-2009 - Official Umbraco Trainer for North America - Percipient Studios
Arjandb
Posted: Sunday, March 09, 2008 7:26:56 PM
Rank: Enthusiast

Joined: 8/31/2007
Posts: 32
Location: Utrecht NL
How do you mean? Using a user control macro instead of a xslt macro?

In that case, it's not so easy to get the @updateDate as it is in xslt.

And I do not like the viewstate en postback code to be inserted as ASPNET_HEAD does. (Thought ASPNET_HEAD isn't needed for a user control as long as no server controls are used in it.)

Or am I missing something? Is there an ASPNET_HEAD option that modifies http headers? I cannot see the relationship yet.
drobar
Posted: Sunday, March 09, 2008 9:54:01 PM

Rank: Umbracoholic

Joined: 9/8/2006
Posts: 1,285
Location: KY, USA
What was I thinking?!? Of course ASPNET_HEAD won't do anything useful for this.

Sorry about that. :blush:

cheers,
doug.

MVP 2007-2009 - Official Umbraco Trainer for North America - Percipient Studios
Users browsing this topic
Guest


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.