0
public void ProcessRequest(HttpContext context)
    {
        string method = context.Request["method"];
        if (!string.IsNullOrEmpty(method))
        {
            switch (method)
            {
                case "getReportGroups":
                    {
                    long projectId = long.Parse(context.Request.Params["projectId"]);
                        if (projectId > 0)
                            SerializeResults(getReportGroups(projectId), null, false, context);
                        else
                        {
                            ThrowInvalidProjectIdException(projectId);
                        }
                    }
}}

我想验证在变量“long projectId”中检索并存储的 projectId。如果假设它作为字符串或其他东西传递,我会遇到异常。那么如何验证呢。

4

1 回答 1

0

如果您不确定投射时它是否会很长,则可以改用 TryParse。

long projectId;
if(long.TryParse(context.Request.Params["projectId"], out projectId)
{
    if (projectId > 0)
        SerializeResults(getReportGroups(projectId), null, false, context);
    else
    {
        ThrowInvalidProjectIdException(projectId);
    }
}
else
{
    //Handle the case where projectId could not be parsed as a long
}
于 2013-06-25T12:15:38.280 回答