ASP.NET MVC 判斷是否為指定頁面的 Helper
- 2009-06-03
- 14921
- 0
最近案子經常需要判斷目前頁面是否是指定的頁面然後動態顯示一些什麼出來,之前都是想到一個寫一個判斷所以到後來還滿雜亂的,今天好不容易把案子趕完了空閒的時間就來整理一下這個我覺得我會很經常性用到的Helper。
需求很簡單,我只是要知道現在的View是那一個用於判斷我是不是應該顯示某些資料或是載入某些欄位給使用者觀看,之前的〔ASP.NET MVC 如何判斷現在是哪一頁〕一文中介紹的寫法相當的陽春,demo在實務上遇到了多次判斷不來或是很醜陋的判斷,所以想要規劃一下這玩意到底該長什麼樣子,以下就是完整的Code,想要用的記得自己開一個Class來放。
#region 判斷是否為目前頁面
/// <summary>
/// 判斷是否為指定的Controller
/// </summary>
/// <param name="controllerName">指定的controllerName</param>
/// <returns></returns>
public static bool IsCurrentAction(this HtmlHelper helper, string controllerName)
{
return IsCurrentAction(helper, controllerName, null);
}
/// <summary>
/// 判斷是否為指定的Controller中的Action
/// </summary>
/// <param name="actionName">指定的ActionName(可以使用,分隔)</param>
/// <param name="controllerName">指定的ControllerName</param>
/// <returns></returns>
public static bool IsCurrentAction(this HtmlHelper helper, string controllerName, string actionName)
{
string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];
if (string.IsNullOrEmpty(actionName))
{
return currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase);
}
else
{
if (actionName.Contains(","))
{
bool iscurrent = false;
string[] array = actionName.Split(new char[] { ',' });
if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase))
{
foreach (var item in array)
{
if (currentActionName.Equals(item, StringComparison.CurrentCultureIgnoreCase))
{
iscurrent = true;
break;
}
}
}
return iscurrent;
}
else
{
if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase))
return currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase);
}
}
return false;
}
/// <summary>
/// 判斷是否為指定的Controller但排除指定的Action
/// </summary>
/// <param name="actionName">排除的ActionName(可以使用,分隔)</param>
/// <param name="controllerName">指定的ControllerName</param>
/// <returns></returns>
public static bool NotIsCurrentAction(this HtmlHelper helper, string controllerName, string actionName)
{
string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];
if (actionName.Contains(","))
{
bool iscurrent = true;
string[] array = actionName.Split(new char[] { ',' });
if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase))
{
foreach (var item in array)
{
if (currentActionName.Equals(item, StringComparison.CurrentCultureIgnoreCase))
{
iscurrent = false;
break;
}
}
}
return iscurrent;
}
else
{
if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase))
return !currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase);
}
return false;
}
/// <summary>
/// 判斷是否為指定的頁面
/// </summary>
/// <param name="actionName">指定的ActionName</param>
/// <param name="controllerName">指定的ControllerName</param>
/// <param name="IdName">指定的Id</param>
/// <returns></returns>
public static bool IsCurrentPage(this HtmlHelper helper, string controllerName, string actionName, string IdName)
{
string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];
string currentIdName = (string)helper.ViewContext.RouteData.Values["id"];
if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) &&
currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase) &&
currentIdName.Equals(IdName, StringComparison.CurrentCultureIgnoreCase))
return false;
return true;
}
#endregion
以上這些Code可以判別出以下幾種情況
- 指定的Controller
- 指定的Controller下的某一個Action
- 指定的Controller下的多個Action
- 指定的Controller但不能是某一個Action
- 指定的Controller但不能是某一些Action
- 指定的Controller下的Action並且參數為xxx
使用範例:
情境一:首頁必須要秀出目前時間
<%
if (Html.IsCurrentAction("home", "Index"))
{
//秀時間
}
%>
情境二:會員頁面中都要秀出會員姓名
<%
if (Html.IsCurrentAction("Member"))
{
//秀會員姓名
}
%>
情境三:會員頁面中都要秀會員姓名但是登入頁不秀
<%
if (Html.NotIsCurrentAction("Member","Logon"))
{
//秀會員姓名
}
%>
情境四:活動頁面中的"細節頁"、"清單頁"要秀出活動主辦人
<%
if (Html.IsCurrentAction("activity", "detail,list"))
{
//秀主辦人
}
%>
情境五:會員頁面中的某一會員必須秀出版主圖示
<%
if (Html.IsCurrentPage("member", "detail","123"))
{
//秀出版主圖示
}
%>
用法大致為這樣加上[反向]會有更多變化@@提供給有需要的朋友參考吧。








回應討論