Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

c# - '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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...