-1
I am getting error while trying to call a method from a Web API.
Declaration of the method in ApiController
public class AcessoController : ApiController
{
[HttpGet]
public UsuarioModel ValidarUsuario(string login, string senha, string modoAutenticacao)
{
}
}
Consuming the API
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(ConfigurationManager.AppSettings["UrlAPI"]);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = new HttpResponseMessage();
response = await client.GetAsync(string.Format("api/Acesso/ValidarUsuario?login={0}&senha={1}&modoAutenticacao={2}", model.Login, model.Senha, (model.ModoAutenticacao == LoginViewModel.Dominio.Computecnica) ? "interno" : "externo"));
if (response.IsSuccessStatusCode) //esta retornando falso
{
}
Error:
Cannot be null Parameter name: root
It seems to be fault in the construction of the URL, since the execution of this method worked through Unittest.
This is bizarre. API consumes itself?
– Leonel Sanches da Silva
Seems to be missing the method when consuming the API? For example:
client.GetAsync(string.Format("api/Acesso/ValidarUsuario?login={0}...
(includes "Validarusuario" in the text)– Rick Wolff
I have been looking at this example and it seems to me that the name of the method is not given. It appears he identifies which method to call, according to past parameters. http://csharp-video-tutorials.blogspot.com.br/2016/09/aspnet-web-api-query-string-parameters.html
– Otavio Camargo
Anyway, I just made a test by putting the name of the method, but it still hasn’t worked. Code is updated in the main question.
– Otavio Camargo
@Ciganomorrisonmendez is not the same application. I have a WEB API project that validates users, and an MVC application that consumes this Web API
– Otavio Camargo
Does it keep giving error? Or now just returns
false
? Investigating the objectresponse
, vc has the answer HTTP code?– Rick Wolff
The same error continues. I tried calling the url directly from the browser: http://localhost:52337/api/Access/Validationroom? login=cpt&password=cpt&modoAutentico=external Error: Cannot be null Parameter name: root
– Otavio Camargo
I was going to tell you to do it. : ( I did a search in English and this error only appears to those who are using Ninject. You are using Ninject?
– Rick Wolff
Yes, this one has the same coding as the MVC project, because they both share the same data access project, which is encoded with dependency injection.
– Otavio Camargo
Maybe the solution to these posts can help you: http://stackoverflow.com/questions/3703903/wcf-with-ninject-throwing-argumentnullexception or http://stackoverflow.com/questions/5451580/ninject-wcf-extension-argumentnullexception-using-net-tcp-Binding They talk about upgrading libraries to newer version...
– Rick Wolff
Thanks for the help. Searching on the subject, I discovered that the problem was in the lack of installation of the package Webactivaotorex, Ninjectmvc5.
– Otavio Camargo