如何返回以下值?

时间:2018-07-09 08:18:46

标签: c# .net

我正在做我的项目,但是我不能继续。我的项目应生成两个代码部分,并将它们转换为两个哈希。多数民众赞成在工作。但是现在,我想在浏览器中打印出这些值。

这是我未完成的代码:

模型:

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;
    }

}
}

在我的演示控制器中,我想返回值CodePartACodePartBHashABHashBA

 // GET api/demo/code
 [HttpGet]
 [Route("code")]
 public string Code()
 {
   //return values here
 }

谢谢您的帮助!

欢呼

2 个答案:

答案 0 :(得分:0)

就像它应该工作:

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

答案 1 :(得分:0)

您应该以api方法返回IHttpActionResult接口。

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