【问题标题】:Calling angular 4 router with Multiple parameters from C#从 C# 调用具有多个参数的 Angular 4 路由器
【发布时间】:2018-07-31 14:47:51
【问题描述】:

我想使用 C# 调用具有多个参数的路由器。这是我正在制作的 url

 var _email = UtilityConverter.Encrypt(Email);
            var _userId = UtilityConverter.Encrypt(Convert.ToString(UserId));

            // this.router.navigate(['search', { term: term}]);

            var _subject = "Email Verification";
            StringBuilder sb = new StringBuilder();
            sb.Append("<html>");
            sb.Append("<body>");
            sb.Append("<p>Dear ,</p>");
            sb.Append("<p>Please click the below link to verify your email.</p><br>");
            sb.Append("<a href='http://localhost:4200/Emailverification/" + _email + "/" + _userId + "'>click here</a>");
            sb.Append("<p>Sincerely,<br>-Offey</br></p>");
            sb.Append("</body>");
            sb.Append("</html>");
            return MailUtilityHelper.SendMail(Email, _subject, sb.ToString());

我的电子邮件和用户 ID 已加密导致问题。

这是我的多参数路由器

 {path:'Emailverification/:email/:userid', component:EmailVerficationComponent,canActivate : [AuthguardComponent]},

任何帮助将不胜感激。在控制台中给我以下错误。

【问题讨论】:

    标签: c# angular webapi2


    【解决方案1】:

    您只向路由器传递了一个参数,即 Emailverification/something

    在您的情况下,您必须将两个参数都填充到 url。 如果你想让路由参数为空,你应该声明另一条带有一个参数的路由和另一条没有参数的路由,应该是

     {path:'Emailverification', component:EmailVerficationComponent,canActivate : [AuthguardComponent]},
     {path:'Emailverification/:userid', component:EmailVerficationComponent,canActivate : [AuthguardComponent]},
     {path:'Emailverification/:email', component:EmailVerficationComponent,canActivate : [AuthguardComponent]},
    

    或者你可以在你的组件中发现错误:EmailVerficationComponent 组件。

    【讨论】:

    • localhost:4200/Emailverification/… 这是我的网址,它给了我以下错误
    • 根据您上传的照片,您的网址只有一个参数,在您的网址中只有一个/(斜杠)
    • 是的,但我都通过了,我很确定
    • aha ,由于 = 字符,angular 无法在其路由 url 中理解 =
    • 我认为最好的方法是删除后端中的特殊字符并将干净的 url 发送给客户端。
    【解决方案2】:

    您应该使用其他加密方法来加密您的电子邮件和用户名。我将为您提供没有特殊字符的加密和解密方法。这将解决您的问题。

           public static string Encrypt(string stringvalue, System.Text.Encoding encoding)
        {
            Byte[] stringBytes = encoding.GetBytes(stringvalue);
            StringBuilder sbBytes = new StringBuilder(stringBytes.Length * 2);
            foreach (byte b in stringBytes)
            {
                sbBytes.AppendFormat("{0:X2}", b);
            }
            return sbBytes.ToString();
        }
        public static string Decrypt(string hexvalue, System.Text.Encoding encoding)
        {
            int CharsLength = hexvalue.Length;
            byte[] bytesarray = new byte[CharsLength / 2];
            for (int i = 0; i < CharsLength; i += 2)
            {
                bytesarray[i / 2] = Convert.ToByte(hexvalue.Substring(i, 2), 16);
            }
            return encoding.GetString(bytesarray);
        }
    

    希望这对您有所帮助,虽然它不是优化解决方案,但它可以帮助您解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-15
      • 2017-07-19
      • 1970-01-01
      • 2020-11-16
      • 2013-11-27
      • 1970-01-01
      • 2020-08-30
      • 2019-06-28
      相关资源
      最近更新 更多