【问题标题】:'HttpRequest' does not contain a definition for 'Url'“HttpRequest”不包含“Url”的定义
【发布时间】:2020-12-07 02:24:24
【问题描述】:
public IActionResult Register(SysUser usr)
    {
        if (!ModelState.IsValid)
        {
            ViewData["Message"] = "Invalid Input";
            ViewData["MsgType"] = "warning";
            return View("UserRegister");
        }
        else
        {
            string insert =
               @"INSERT INTO SysUser (UserId, UserPw, FullName, Email, CorporateId, UserRole) VALUES
             ('{0}', HASHBYTES('SHA1', '{1}'), '{2}', '{3}', {4}, 'user')";
            if (DBUtl.ExecSQL(insert, usr.UserId, usr.UserPw, usr.FullName, usr.Email) == 1)
            {
                usr.ActivationToken = Guid.NewGuid().ToString();
                if (usr.ActivationToken != null)
                {
                    string inst = @"INSERT INTO SysUser(Active)VALUES('1')";
                    if (DBUtl.ExecSQL(inst, usr.Active) == 1)
                    {



                        string template = @"Hi {0},<br/><br/>
                           you have successfully created an account at Parkway Pantai Health Screening Portal.
Please click on the link below to verify your email address and complete your registration. Your user id is <b>{1}</b> and password is <b>{2}</b>.
                           <br/>";

                        template+= "</br><a href= '" + Request.Url.AbsoluteUri.Replace("CS.aspx", "CS_Activation.aspx?ActivationToken"+ usr.ActivationToken
                            ) + "'>Click here to activate your account.</a>";


                        string title = "Registration Successul - Welcome";
                        string message = String.Format(template, usr.FullName, usr.UserId, usr.UserPw);
                        string result = "";

                        bool outcome = false;
                        // TODO: L10 Task 2b - Call EmailUtl.SendEmail to send email
                        //                     Uncomment the following line with you are done
                        outcome = EmailUtl.SendEmail(usr.Email, title, message, out result);
                        if (outcome)
                        {
                            ViewData["Message"] = "User Successfully Registered";
                            ViewData["MsgType"] = "success";
                        }
                        else
                        {
                            ViewData["Message"] = result;
                            ViewData["MsgType"] = "warning";
                        }
                    }
                    else
                    {
                        ViewData["Message"] = DBUtl.DB_Message;
                        ViewData["MsgType"] = "danger";
                    }
                }
            }
            return View("UserRegister");
        }
    }

大家好,我正在尝试使用 Guid.NewGuid() 发送激活链接,当我想使用“Request.Url.AbsoluteUri.Replace”生成链接时,它会显示错误:

“HttpRequest”不包含“Url”的定义,并且找不到接受“HttpRequest”类型的第一个参数的可访问扩展方法“Url”(您是否缺少 using 指令或程序集引用?)。

有什么解决方法吗?我使用的框架是 c# .net framework 3.1。提前感谢您的帮助!

【问题讨论】:

  • 您是否正在尝试生成您正在运行的网站的基本网址?

标签: c#


【解决方案1】:

Related question about how to process verification after sending activation link.

您可以使用以下方法生成网站的基本网址:

string baseUrl = string.Format("{0}://{1}", 
                       HttpContext.Request.Scheme, HttpContext.Request.Host);

HttpContext.Request.Scheme 返回http/https
HttpContext.Request.Host 是主机,即(localhost 或 www.example.com)。
所以基本上你会得到https://www.example.com

然后,您可以将Guid 组合为查询字符串并将其发送给用户。

var activationUrl = $"{baseUrl}/Controller/Action?code={Guid.NewGuid()}";

注意:我使用变体来组合字符串只是为了得到答案。 Later 从 C# 6 开始可用。

【讨论】:

  • @CamiloTerevinto 谢谢 - 已更新。对于所有类似类型的对象,这里不需要。但是Object[] 可以像documentation 一样被传递。我刚刚从我们的一个项目中复制并粘贴到这里。
  • 是的,当然是有效的,这是params 内部所做的;然而,在我编程的 7 年里,我从未见过这种语法,所以它让我觉得很奇怪。此外,最多可有 3 个参数的重载,因此创建数组会破坏这里的好处
  • @CamiloTerevinto 是的。我通常避免在 param 中使用数组(并且尽可能在任何地方使用)。但这是几年前由另一位开发人员编写的,它只是按原样保留,因为它不会改变不同项目之间的行为。但是了解不必要的编码方式总是好的。感谢您引起我的注意。
  • @Pirate 非常感谢您提供这条信息,我设法整合并成功运行
  • @ImtryingtogetAforprojplshelp 很高兴它有帮助,干杯!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-27
相关资源
最近更新 更多