2009-08-09 124 views
5

我有一個奇怪的問題,其中ViewContext.RouteData.Values [「action」]在我的登臺服務器上爲null,但在我的開發機器(asp.net開發服務器)上工作正常。ViewContext.RouteData.Values [「action」]在服務器上爲空...在本地機器上正常工作

代碼很簡單:

public string CheckActiveClass(string actionName) 
    { 
     string text = ""; 
     if (ViewContext.RouteData.Values["action"].ToString() == actionName) 
     { 
      text = "selected"; 
     } 
     return text; 
    } 

我上ViewContext.RouteData.Values錯誤[ 「行動」]行。錯誤是:

異常詳細信息:System.NullReferenceException:未將對象引用設置爲對象的實例。

任何幫助表示讚賞。提前致謝。

+0

您是否找到任何解決方案?我現在有類似的問題... – 2010-09-07 12:16:58

+0

不,我沒有,我最終改變了邏輯和它的工作方式。 – rksprst 2010-09-07 23:19:50

回答

2

你的開發和登臺服務器上是否有不同版本的asp.net mvc?嘗試將System.Web.Mvc本地複製到登臺服務器並查看是否修復它。 (右鍵單擊參考,選擇屬性,並將複製本地更改爲true)

這可能會也可能不會幫助您的情況,但此處是我從asp.net/mvc上的MVC模板偷取的助手擴展:

/// <summary> 
/// Checks the current action via RouteData 
/// </summary> 
/// <param name="helper">The HtmlHelper object to extend</param> 
/// <param name="actionName">The Action</param> 
/// <param name="controllerName">The Controller</param> 
/// <returns>Boolean</returns> 
public static bool IsCurrentAction(this HtmlHelper helper, string actionName, string controllerName) 
{ 
    string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"]; 
    string currentActionName = (string)helper.ViewContext.RouteData.Values["action"]; 

    if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase)) 
     return true; 

    return false; 
} 
+0

我剛剛用本地副本重新發布,並且我得到相同的錯誤... – rksprst 2009-08-09 21:18:41

+0

也許嘗試上述:System.Web.Abstractions,System.Web.Extensions,System.Web.Mvc和System.Web.Routing?我認爲這些是asp.net使用的四種引用。 – 2009-08-10 00:48:20

+0

當我重新發布時,我在所有MVC相關DLL上做了「複製本地」......所以這些加上一些其他常規的asp.net DLL以防萬一。我只是不明白爲什麼它可以在dev上運行,而不是在分段服務器上運行。 – rksprst 2009-08-10 04:43:16

0

我不能,爲什麼它的工作原理同一個地方,而不是其他,而是說:

  1. 你應該向上突破碼成幾行搞清楚到底什麼是空(VAR路線= ViewContext.RouteData; var values = ...;)等。

  2. 你從哪裏調用CheckActiveClass?在什麼時候?它位於哪裏? ViewContext並不總是隨處可見。但是你可以更好地瞭解#1後沒有的東西。

詹姆斯

+0

我在視圖的代碼隱藏中使用它。以及主頁的代碼隱藏。 我會分解它,看看它的錯誤。這很奇怪,因爲它在開發機器上工作,但不在登臺服務器上。 – rksprst 2009-08-09 22:50:36

0

嘗試使用大寫

字符串currentController = ViewContext.RouteData.Values [ 「控制器」]的ToString(); String currentAction = ViewContext.RouteData.Values [「Action」]。ToString(); String currentID = ViewContext.RouteData.Values [「ID」]。ToString();

相關問題