How to call some Controller's method and pass a parameters from a query string

1.7k Views Asked by At

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 [email protected] and so on..

There are informations about my website:

www.test.com

mail:[email protected]

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

1

There are 1 best solutions below

0
AudioBubble On BEST ANSWER

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