'HttpRequest' does not contain a definition for 'Url'

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

Hi all, I am trying to send a activation link using Guid.NewGuid() and when I want to generate a link using "Request.Url.AbsoluteUri.Replace" it shows a error where it says:

'HttpRequest' does not contain a definition for 'Url' and no accessible extension method 'Url' accepting a first argument of type 'HttpRequest' could be found (are you missing a using directive or an assembly reference?).

Is there any workaround to this? The framework i am using is c# .net framework 3.1. Thanks in advance for the help!

Upvotes: 1

Views: 5826

Answers (1)

Pirate
Pirate

Reputation: 1175

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

You can generate the base url of the site using the following:

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

where HttpContext.Request.Scheme returns http/https
and HttpContext.Request.Host is the host i.e.(localhost or www.example.com).
So basically you will get https://www.example.com

Then, you can combine the Guid as a query string and send it to the user.

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

Note: I've used variation to combine strings just for the answer. Later is available starting with C# 6.

Upvotes: 4

Related Questions