0

in my app I've generated an url like this:

http://www.test.com/?mail=test%40gmail.ba&code=71147ff9-87ae-41fc-b53f-5ecb3dbe5a01

The way how I generated Url is posted below:

private string GenerateUrl(string longUrl, string email, string confirmCode)
{
    try
    {
        // By the way this is not working (Home/MailConfirmed) I'm getting message 
        // Requested URL: /Home/MailConfirmed
        // The resource cannot be found.
        string url = longUrl + "/Home/MailConfirmed";
        var uriBuilder = new UriBuilder(url);
        var query = HttpUtility.ParseQueryString(uriBuilder.Query);
        query["mail"] = email;
        query["code"] = confirmCode;
        uriBuilder.Query = query.ToString();
        uriBuilder.Port = -1;
        url = uriBuilder.ToString();
        return url;
    }
    catch (Exception ex)
    {
        return "Error happened: " + ex.Message;
    }
}

In longUrl I'm passing www.test.com, in email I'm passing test@gmail.com and so on..

There are informations about my website:

www.test.com

mail:test@gmail.com

confirmcode:71147ff9-87ae-41fc-b53f-5ecb3dbe5a01

And in my HomeController.cs there is a method which should took parameters out of query string - url and pass it to the method which should activate users account by getting user by mail (mail is unique) and comparing this guid with guid in database. So I'm wondering how can I call this method?

So my method looks like this:

 public  JsonResult MailConfirmed(string mail, string confirmCode)
 {
       try
       {
           // Here I will get user and update it in DB
              return Json("success", JsonRequestBehavior.AllowGet);
       }
       catch(Exception ex)
       {
           return Json("fail", JsonRequestBehavior.AllowGet);
       }
  }

So my question is how is possiblee for user to click on following link and to get an my method invoked.. ?

Thanks a lot Cheers

billy_56
  • 649
  • 3
  • 11
  • 27
  • Your url needs to be `http://www.test.com/Home/MailConfirmed?mail=test%40gmail.ba&confirmcode=71147ff9-87ae-41fc-b53f-5ecb3dbe5a01` –  May 03 '18 at 21:35
  • @StephenMuecke so in UriBuilder I should write an www.test.com/Home/MailConfirmed + parameters? :o – billy_56 May 03 '18 at 21:38
  • Why are you using `UriBuilder` (as opposed to using `Url.Action()`)? –  May 03 '18 at 21:41
  • @StephenMuecke I need to send an url which should redirect user to method which should activate his account, so I guess that was the way.. do you want me to post full code how I created Url so we might edit it and you might post it as answer so I can accept it.. ? – billy_56 May 03 '18 at 21:42
  • Sure (and by _send an url_, do you mean you sending an email to a user with the link?) –  May 03 '18 at 21:44
  • @StephenMuecke Yes I do, for example user press register, after that mail with activation link is sent to user, user has to click on it so I can update database with field EmailConfirmed : true :) – billy_56 May 03 '18 at 21:47
  • @StephenMuecke take a look mate – billy_56 May 03 '18 at 21:52
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170322/discussion-between-stephen-muecke-and-billy-56). –  May 03 '18 at 21:54

1 Answers1

1

In order to navigate to your MailConfirmed(), your url would need to be

http://www.test.com/Home/MailConfirmed?mail=test%40gmail.ba&confirmcode=71147ff9-87ae-41fc-b53f-5ecb3dbe5a01

Note the segments for the controller and action names, and code=xxx should be confirmcode=xxx to match the name of the parameter in the method.

You can simplify your code (and delete your GenerateUrl() method) by making use of UrlHelper methods to generate the url).

To generate the above url, all you need in your controller method is

string url = Url.Action("MailConfirmed", "Home", 
    new { mail = email, confirmcode = confirmCode },
    this.Request.Url.Scheme);