0

我正在研究一个 AZURE 函数。该函数的目的是计算模块的状态:失败、通过宽恕、通过或通过区别。当我运行代码时,我得到这个错误。我在标记处得到一个错误,因为它是一个整数。

Executed 'Function1' (Failed, Id=90c68ac4-4bc1-4446-9ad4-2b15e8ea8a00, Duration=145230ms)
System.Private.CoreLib: Exception while executing function: Function1. Anonymously Hosted DynamicMethods Assembly: Cannot implicitly convert type 'string' to 'int'.

班上:

class MarkEntry
{
    public string StudentNumber { get; set; }
    public string Name { get; set; }
    public int Mark { get; set; }
    public string Module { get; set; }
}

}

Azure 函数接受 4 个输入并检查学生是否有宽恕通行证:

    [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            MarkEntry obj = new MarkEntry();
            string responseMessage ="";
            string respmaessage="";
            string result="";
            log.LogInformation("C# HTTP trigger function processed a request.");

            obj.StudentNumber = req.Query["studentnumber"];
           obj.Name = req.Query["name"];
           obj.Module = req.Query["module"];
            obj.Mark =Convert.ToInt32(req.Query["mark"]);


            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            obj.StudentNumber = obj.StudentNumber ?? data?.name;
            obj.Name = obj.Name ?? data?.name;
            obj.Module = obj.Module ?? data?.name;
           obj.Mark = obj.Mark.ToString() ?? data?.obj.Mark.Tostring();







            if (string.IsNullOrEmpty(obj.StudentNumber))
            {
                respmaessage = "Enter a student number";
                return new OkObjectResult(respmaessage);

            }


            if (string.IsNullOrEmpty(obj.Name))
            {
                respmaessage = "Enter a student Name";
                return new OkObjectResult(respmaessage);

            }
         
            
            if (string.IsNullOrEmpty(obj.Module))
            {
                respmaessage = "Enter Student Module";
                return new OkObjectResult(respmaessage);

            }


            if (obj.Mark<49)
            {
                result = "Condoned Pass";

            }

            responseMessage = string.Format($"Student Number: {obj.StudentNumber}" + "\n" +
$"Student Name: {obj.Name}" + "\n" + $"Student Mark: {obj.Mark}" + "\n" +
$"Student Module: {obj.Module}" + "\n" + $"Student Result: {result}"
                );
              

            return new OkObjectResult(responseMessage);

        }
    }
4

1 回答 1

0

该类MarkEntry具有Mark定义为整数类型的属性。在 azure 函数中,我们实例化了这个类。...

MarkEntry obj = new MarkEntry();

然后在函数的第 13 行左右......

obj.Mark = obj.Mark.ToString() ?? data?.obj.Mark.Tostring();

看起来我们正试图为这个整数分配一个字符串值。不确定这会起作用吗?

于 2020-09-20T02:29:26.030 回答