Nathiel Barros
Nathiel Barros

Reputation: 715

How to read string parameters and forms data from a url Aspnet WebApi

1 - I generate a Link from a WebApi Controller passing two parameters with:

var URL = new Uri( Url.Link("Default", new { Controller = "Reset", Action = 
"ResetPassword",user = "AAAAA", hash = "HASHVALUE" }));

And I get this:

http://localhost:52494/Reset/ResetPassword?user=BBBBBBBB&hash=AAAAAAA

2 - I need to Read these two parameters and the Form as well.

So I have a controller Reset with a ResetPassword Action:

public ActionResult ResetPassword()
{
    return View();
}

[HttpPost]
public ActionResult ResetPassword(Models.ResetUserModel user)
{
   var HASH = Request["hash"];     
   var id = Request["user"];
   return View();
}

And the cshtml file:

enter image description here

If I run this page and fill the form, I will be able to read the ResetUserModel, but if it has some parameter, the model comes null!!

What am I doing wrong here?

Upvotes: 1

Views: 191

Answers (2)

Laxman Gite
Laxman Gite

Reputation: 2318

It seems like you haven't defined related parameters in your action because you are passing two parameters from URI :

user = "AAAAA", hash = "HASHVALUE"

var URL = new Uri( Url.Link("Default", new { Controller = "Reset", Action = 
"ResetPassword",user = "AAAAA", hash = "HASHVALUE" }));

So you have to define you action according to that like :

[HttpGet]
public ActionResult ResetPassword(string user, string hash)
{
   var user = user;     
   var hash = hash;

   return View();
}

Upvotes: 3

meJustAndrew
meJustAndrew

Reputation: 6603

Instead of using a HttpPost try to use a Get method:

[HttpGet]
public ActionResult ResetPassword(string user, string hash)
{
   //populate the model with the user and hash values
   var model = new ResetUserModel();
   model.User = user;
   model.Hash = hash;
   return View(model);
}

You will be able to invoke the ResetPassword with your link:

http://localhost:52494/Reset/ResetPassword?user=BBBBBBBB&hash=AAAAAAA

Upvotes: 0

Related Questions