[英]ViewContext.RouteData.Values[“action”] is null on server… works fine on local machine
I'm having a weird issue where ViewContext.RouteData.Values["action"] is null on my staging server, but works fine on my dev machine (asp.net development server). 我有一个奇怪的问题,其中ViewContext.RouteData.Values [“action”]在我的登台服务器上为空,但在我的开发机器(asp.net开发服务器)上工作正常。
The code is simple: 代码很简单:
public string CheckActiveClass(string actionName)
{
string text = "";
if (ViewContext.RouteData.Values["action"].ToString() == actionName)
{
text = "selected";
}
return text;
}
I get the error on the ViewContext.RouteData.Values["action"] line. 我在ViewContext.RouteData.Values [“action”]行上收到错误。 The error is: 错误是:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. 异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。
Any help is appreciated. 任何帮助表示赞赏。 Thanks in advance. 提前致谢。
Do you have different versions of asp.net mvc on your dev and staging servers? 你的开发和登台服务器上有不同版本的asp.net mvc吗? Try copying System.Web.Mvc locally to the staging server and see if that fixes it. 尝试将System.Web.Mvc本地复制到登台服务器,看看是否修复了它。 (Right click on the reference, choose properties, and change Copy Local to true) (右键单击引用,选择属性,然后将Copy Local更改为true)
This may or may not help your situation, but here is a helper extension I stole from an MVC template on asp.net/mvc: 这可能会或可能不会对您的情况有所帮助,但这是我从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;
}
I can't speak to why it works one place and not another, but: 我不能说为什么它适用于一个地方而不是另一个地方,但是:
You should break the code up into several lines to figure out exactly what is null (var route = ViewContext.RouteData; var values = ...;), etc. 您应该将代码分成几行,以确切地知道什么是null(var route = ViewContext.RouteData; var values = ...;)等。
Where are you calling CheckActiveClass from? 你在哪里打电话给CheckActiveClass? At what time? 在什么时候? Where's it located? 它在哪里? ViewContext isn't always available everywhere. ViewContext并不总是随处可用。 But you'll have a better idea of what's not available after #1. 但是你会更好地了解#1之后不可用的东西。
James 詹姆士
Try using capitals 尝试使用大写字母
String currentController = ViewContext.RouteData.Values["Controller"].ToString(); String currentController = ViewContext.RouteData.Values [“Controller”]。ToString(); String currentAction = ViewContext.RouteData.Values["Action"].ToString(); String currentAction = ViewContext.RouteData.Values [“Action”]。ToString(); String currentID = ViewContext.RouteData.Values["ID"].ToString(); String currentID = ViewContext.RouteData.Values [“ID”]。ToString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.