简体   繁体   English

ViewContext.RouteData.Values [“action”]在服务器上为空...在本地计算机上正常工作

[英]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: 我不能说为什么它适用于一个地方而不是另一个地方,但是:

  1. 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 = ...;)等。

  2. 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.

相关问题 ViewContext.RouteData.Values[&quot;Controller&quot;] 始终为 Null - Asp.Net Core 2.1 - ViewContext.RouteData.Values["Controller"] always Null - Asp.Net Core 2.1 ASP.NET MVC 5 路由 - 创建 Html.ActionLink 并解析 ViewContext.RouteData.Values - ASP.NET MVC 5 routing - create Html.ActionLink and parse ViewContext.RouteData.Values Ajax自动完成CSS在iis和服务器上工作正常,但在本地计算机上工作不正常 - Ajax Auto complete Css works fine in iis and the server but not in local machine RouteData.Values空重定向 - RouteData.Values null redirect 当在本地计算机上正常运行时,为什么在IIS 7服务器上找不到类型或名称空间? - Why can't the type or namespace be found on IIS 7 server when it works fine on the local machine? 与Action Parameters结合使用RouteData.Values - Marry RouteData.Values with Action Parameters C# 应用程序在服务器上工作正常但在客户端机器上抛出 null 异常错误 - C# application works fine on server but throws null exception error on client machine 不在azure服务器上工作但在本地环境中工作正常吗? - Not working on azure server but works fine in local environment? DirectX 应用程序无法通过 Process.Start 在 Windows Server 2012 R2 上加载,通过 CMD 或在本地计算机上正常工作 - DirectX app unable to load through Process.Start on Windows Server 2012 R2, works fine through CMD or on local machine .Ajax方法可在本地计算机上使用,但不能在服务器上使用 - .Ajax method works on local machine, but not on server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM