C# 值不能为空。参数名称:字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13702212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:29:10  来源:igfitidea点击:

Value cannot be null. Parameter name: String

c#exceptionhandlerrequest.querystringnul

提问by Kush

Please take a look at the following code. It's in handler.asxh.

请看下面的代码。它在handler.asxh.

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "application/json";
    new RequestManagementFacade().PinRequest(Int32.Parse(context.Request.QueryString["requestId"]), (Boolean.Parse(context.Request.QueryString["isPinned"])));
}

This is showing the following error:

这显示以下错误:

Value cannot be null. Parameter name: String

There is value being passed as I have checked the context request query string, however, the code breaks at this stage.

由于我检查了上下文请求查询字符串,因此传递了值,但是,代码在此阶段中断。

This handler will connect to the business logic layer.

此处理程序将连接到业务逻辑层。

采纳答案by Jon Skeet

There is value being passed as i have checke dthe context request query string

由于我检查了上下文请求查询字符串,因此传递了值

I strongly suspect your diagnostics are incorrect then. Values don't magically go missing - you need to question your assumptions. This is easy to debug through though. I would suggest changing your code to:

我强烈怀疑您的诊断不正确。价值观不会神奇地消失——你需要质疑你的假设。不过,这很容易调试。我建议将您的代码更改为:

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "application/json";
    string requestId = context.Request.QueryString["requestId"];
    string isPinned = context.Request.QueryString["isPinned"];
    var facade = new RequestManagementFacade();
    facade.PinRequest(Int32.Parse(requestId), Boolean.Parse(isPinned));
}

It's then reallysimple to step through and find out what's going on.

然后就可以容易地逐步完成并找出发生了什么。

回答by Levi Botelho

It is likely that either context.Request.QueryString["requestId"]or context.Request.QueryString["isPinned"]is not returning a valid string value. Check that both values are passed in the query string with the proper IDs, those being of course requestIdand isPinned.

很可能要么context.Request.QueryString["requestId"]或未context.Request.QueryString["isPinned"]返回有效的字符串值。检查两个值是否都通过正确的 ID 在查询字符串中传递,这些当然是requestIdisPinned

回答by Kush

Okay solved when passing the values to the handler i inserted it as

好的,当将值传递给处理程序时解决了,我将其插入为

"PinRequest.ashx?="+requestId+isPinned"

Which gave me the result 2True

这给了我结果 2True

So i realised the hiccup was with not including the string names

所以我意识到打嗝是不包括字符串名称

"PinRequest.ashx?requestId=" + this._requestId + "&isPinned=" + this._isPinned

Thanks for you help guys

谢谢你们的帮助

LeviBotelho Thank you made me check something i was missing out when checking as its javascript

LeviBotelho 谢谢你让我检查一些我在检查 javascript 时遗漏的东西