Click here to Skip to main content
13,197,548 members (47,234 online)

Web Development

 
GeneralRe: SVG in IE8 Pin
ThetaClear29-Oct-13 21:47
memberThetaClear29-Oct-13 21:47 
AnswerRe: SVG in IE8 Pin
Paulo Augusto Künzel4-Nov-13 3:43
professionalPaulo Augusto Künzel4-Nov-13 3:43 
GeneralRe: SVG in IE8 Pin
ThetaClear5-Nov-13 10:08
memberThetaClear5-Nov-13 10:08 
GeneralRe: SVG in IE8 Pin
Paulo Augusto Künzel5-Nov-13 11:47
professionalPaulo Augusto Künzel5-Nov-13 11:47 
Question(HTML5 + Javascript + javascript Libs) or Razor Engine Pin
OmniSource28-Oct-13 22:48
memberOmniSource28-Oct-13 22:48 
Questionhow to password reminder Pin
Jassim Rahma28-Oct-13 11:00
memberJassim Rahma28-Oct-13 11:00 
AnswerRe: how to password reminder Pin
Richard Deeming28-Oct-13 12:33
professionalRichard Deeming28-Oct-13 12:33 
QuestionASP Net WebAPI Newby Question Pin
Kevin Marois28-Oct-13 6:14
memberKevin Marois28-Oct-13 6:14 
I am VERY new to using WepAPI, and I have a problem. This might be a bit long, but I'm not sure what to post...

I'm using RestSharp....

I created a controller called AppSecurityController. It wraps calls to my security API. In it is a Login method:

The problem is that in the Login method, the Credential parameter is always null.

[System.Web.Http.HttpGet]
public LoginResponse Login(FMG.Entities.CredentialEntity Credentials)
{
    Marois.Common.AppSecurity.Entities.CredentialEntity credentials = new Marois.Common.AppSecurity.Entities.CredentialEntity
    {
        UserName = Credentials.UserName,
        Password = Credentials.Password
    };
 
    LoginResponse response = appSecurityBL.Login(credentials);
           
    return response;
}


My solution has a project called Entities. In it is the Credential class:

[Serializable]
[DataContract]
public class CredentialEntity : _BaseEntity
{
    [DataMember]
    public string Password { get; set; }
 
    [DataMember]
    public string UserName { get; set; }
}


In my client project I have local models, which also has a CredentialModel:
namespace FMG.UI.WPF.Shared.Models
{
  public class CredentialModel : _BaseModel
  {
      public string Password { get; set; }
 
      public string UserName { get; set; }
  }
}


I call to the WebAPI through a proxy class:

public LoginResponseModel Login(CredentialModel credentials)
{
    WebAPIExecutor webAPIExecutor = new WebAPIExecutor("/AppSecurity/Login/", Method.GET);
 
    webAPIExecutor.AddParameter(credentials);
 
    LoginResponseModel results = webAPIExecutor.Execute<LoginResponseModel>();
 
    return results;
}


WebAPIExecutor is a class for handling the RestSharp stuff:

public class WebAPIExecutor
{
    #region Private Fields
    private RestClient client;
    private RestRequest request;
    #endregion
 
    #region CTOR
    public WebAPIExecutor(string url, Method method = Method.POST)
    {
        if (string.IsNullOrEmpty(url))
        {
            throw new ArgumentNullException("Url");
        }
 
        // Set up the URL to the server
        setupServerURL();
 
        client = new RestClient(ServerAddress);
 
        client.AddHandler("text/plain", new JsonDeserializer());
 
        request = new RestRequest(url, method)
        {
            RequestFormat = RestSharp.DataFormat.Json
        };
    }
    #endregion
 
    #region Public Methods
    public void AddParameter(object value, string name = "")
    {
        if (value == null)
        {
            throw new ArgumentNullException("value");
        }
 
        Type type = value.GetType();
        bool isPrimitive = type.IsPrimitive;
 
        if(!string.IsNullOrEmpty(name) || (isPrimitive || type == typeof(string) || type == typeof(decimal)))
        {
            if (string.IsNullOrEmpty(name) && request.Method == Method.GET)
            {
                throw new ArgumentNullException("Parameter 'Name' cannot be empty for Get requests");
            }
 
            request.AddParameter(name, value);
        }
        else
        {
            request.AddBody(value);
        }
    }
 
    public T Execute<T>() where T : new()
    {
        IRestResponse<T> result = client.Execute<T>(request);
 
        int resultValue = (int)result.StatusCode;
 
        if (resultValue >= 299)
        {
            string message = string.Format("An error occured calling the WebAPI. {0} The status code is '{1}'. {2} The error message is {3}", 
                                            Environment.NewLine, result.StatusCode, Environment.NewLine, result.Content );
            MessageBox.Show(message, "WebAPI Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
 
            throw new Exception(message);
        }
 
        return result.Data;
    }
 
    public void Execute()
    {
        IRestResponse result = null;
 
        try
        {
            result = client.Execute(request);
 
            //TODO: Handle this differently.
            int resultCode = (int)result.StatusCode;
 
            //if (result.StatusCode != HttpStatusCode.OK)
            if (resultCode > 299)
            {
                string message = "the error message";  //<== omitted for brevitry
                throw new Exception(message);
            }
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    #endregion
}
 


The parameter is being added with AddParameter and appears to be correct. The controller method is marked as HttpGet.

When I call from the proxy, through the WebAPIExecutor, I reach the controller and the parameter is null.

I'm am really clueless as to why the parameter is null in the controller. Can someone see what I'm doing wrong?

Many thanks
If it's not broken, fix it until it is

Questionget $_GET["id"] in window.location in PHP Pin
Jassim Rahma27-Oct-13 23:49
memberJassim Rahma27-Oct-13 23:49 
SuggestionRe: get $_GET["id"] in window.location in PHP Pin
Richard MacCutchan28-Oct-13 2:09
mvpRichard MacCutchan28-Oct-13 2:09 
AnswerRe: get $_GET["id"] in window.location in PHP Pin
Killzone DeathMan9-Jan-14 1:11
groupKillzone DeathMan9-Jan-14 1:11 
Questionwhy getting Strict Standards: Only variables should be passed by reference? Pin
Jassim Rahma27-Oct-13 23:20
memberJassim Rahma27-Oct-13 23:20 
Questionexpire login's session Pin
Jassim Rahma27-Oct-13 0:06
memberJassim Rahma27-Oct-13 0:06 
QuestionRe: expire login's session Pin
Richard MacCutchan27-Oct-13 2:07
mvpRichard MacCutchan27-Oct-13 2:07 
SuggestionRe: expire login's session Pin
thatraja28-Oct-13 1:02
mvpthatraja28-Oct-13 1:02 
Questionprofile address url validation Pin
Jassim Rahma26-Oct-13 11:05
memberJassim Rahma26-Oct-13 11:05 
AnswerRe: profile address url validation Pin
ProgramFOX26-Oct-13 23:43
member ProgramFOX26-Oct-13 23:43 
GeneralRe: profile address url validation Pin
Jassim Rahma26-Oct-13 23:56
memberJassim Rahma26-Oct-13 23:56 
AnswerRe: profile address url validation Pin
ProgramFOX27-Oct-13 5:39
member ProgramFOX27-Oct-13 5:39 
AnswerRe: profile address url validation Pin
Bernhard Hiller27-Oct-13 23:24
professionalBernhard Hiller27-Oct-13 23:24 
QuestionNetwork for web hosting Pin
is516726-Oct-13 8:24
memberis516726-Oct-13 8:24 
Questionmake <select> NOT required if checkbox is checked Pin
Jassim Rahma26-Oct-13 2:10
memberJassim Rahma26-Oct-13 2:10 
QuestionYears in Descending Order Pin
Jassim Rahma26-Oct-13 0:53
memberJassim Rahma26-Oct-13 0:53 
AnswerRe: Years in Descending Order Pin
Richard MacCutchan26-Oct-13 21:42
mvpRichard MacCutchan26-Oct-13 21:42 
GeneralRe: Years in Descending Order Pin
Jassim Rahma26-Oct-13 22:54
memberJassim Rahma26-Oct-13 22:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy |
Web01 | 2.8.171020.1 | Last Updated 19 Oct 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid