それぞれのホームページとページを持つ2つのサイトを持つUmbracoのインストールがあるとします。
- コンテンツ(-1)
- ホームページ1(1000)
- ホームページ2(1002)
C#では、現在のノードは次のコマンドで取得できます。
Node currentNode = Node.GetCurrent();
対応するホームノードは次のように見つけることができます
Node currentHome = new Node(int.Parse(currentNode.Path.Split(',')[1]));
ここで、currentNode.Path
-1で始まるコンマ区切りの整数の文字列を返します。つまり、ルート、つまり、すべてのホームページが「ライブ」である、呼び出したマスタールートを返します。
たとえば、ページ2.1のパス値は「-1,1002,1003」です。カンマで分割すると、0、1、2のインデックスが付けられた3つの要素を持つ配列になります。ここで、インデックス1の2番目のノードは、ホームノードのIDを示します。ご覧のとおり、最後のIDは現在のノードのIDです。余談ですが、インデックスはノードのレベルも示しているため、ホームページのレベルは1です。
イントラネット/エクストラネットで使用され、ページが保護されているテンプレートで次のスクリプトを使用しました。訪問者が保護されたページへのリンクをたどると、アクセスが拒否され、メンバーログインのあるホームページにリダイレクトされます。
<%@ Master Language="C#" MasterPageFile="~/umbraco/masterpages/default.master" AutoEventWireup="true" %>
<%@ Import Namespace="umbraco.NodeFactory" %>
<script runat="server" language="CSharp">
protected void Page_Load(object sender, EventArgs e)
{
// prevents template to be run without proper authorisation
Node currentNode = Node.GetCurrent();
Node currentHome = new Node(int.Parse(currentNode.Path.Split(',')[1]));
Boolean HasAccess = umbraco.library.HasAccess(currentNode.Id, currentNode.Path);
Boolean IsProtected = umbraco.library.IsProtected(currentNode.Id, currentNode.Path);
if (IsProtected && !HasAccess)
{
// redirect to ancestor-or-self::HomePage
Response.Status = "403 Forbidden";
Response.Redirect(umbraco.library.NiceUrl(currentHome.Id), true);
}
}
</script>
<asp:Content ContentPlaceHolderID="ContentPlaceHolderDefault" runat="server">
<!-- redirect to home page -->
</asp:Content>