Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何返回以下值?_C#_.net - Fatal编程技术网

C# 如何返回以下值?

C# 如何返回以下值?,c#,.net,C#,.net,我正在做我的项目,但我不能继续了。我的项目应该生成两个代码部分,并将它们转换为两个哈希。这些都起作用了。但是现在,我想在浏览器中打印这些值 Thant是我未完成的代码: 模型: namespace myapplication.test.Backend.Models { public class CodeContainer { public string CodePartA { get; set; } public string Code

我正在做我的项目,但我不能继续了。我的项目应该生成两个代码部分,并将它们转换为两个哈希。这些都起作用了。但是现在,我想在浏览器中打印这些值

Thant是我未完成的代码:

模型:

namespace myapplication.test.Backend.Models
{
     public class CodeContainer
     {
          public string CodePartA { get; set; }
          public string CodePartB { get; set; }
          public string HashAB { get; set; }
          public string HashBA { get; set; }
     }
 }
生成代码和哈希的类:

namespace myapplication.test.Backend.Utilities
{
    public static class VerificationCodeUitillity
    {

        private static string GenerateHash(string input)
        {
           string hash = string.Empty; 
           using (MD5 md5Hash = MD5.Create())
           {
                byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            hash = sBuilder.ToString();
        }

        return hash;
    }

    private static string GenerateCodePart(int lenght)
    {
        const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random code = new Random();
        return new string(Enumerable.Repeat(chars, lenght).Select(s => s[code.Next(s.Length)]).ToArray());
    }

    public static CodeContainer GeneratePVCode() 
    {
        CodeContainer result = new CodeContainer();
        result.CodePartA = GenerateCodePart(4);
        result.CodePartB = GenerateCodePart(4);
        result.HashAB = GenerateHash(result.CodePartA + result.CodePartB);
        result.HashBA = GenerateHash(result.CodePartB + result.CodePartA);

        return result;
    }

}
}
提前谢谢你的帮助


干杯

就像它应该起作用一样:

    // GET api/demo/code
    [HttpGet]
    [Route("code")]
    public CodeContainer PVCodeGen()
    {
        return VerificationCodeUitillity.GeneratePVCode();
    }

您应该在api方法中返回IHttpActionResult接口

// GET api/demo/code
[HttpGet]
[Route("code")]
public IHttpActionResult PVCodeGen()
{
    return this.Ok<CodeContainer>(VerificationCodeUitillity.GeneratePVCode());
}
//获取api/demo/code
[HttpGet]
[路线(“代码”)]
公共IHttpActionResult PVCodeGen()
{
返回此.Ok(VerificationDeuitility.GeneratePVCode());
}

我将开始调用GeneratePVC代码,获取结果并尝试显示返回的CodeContainer实例的属性。@steve你能举个例子吗?
// GET api/demo/code
[HttpGet]
[Route("code")]
public IHttpActionResult PVCodeGen()
{
    return this.Ok<CodeContainer>(VerificationCodeUitillity.GeneratePVCode());
}