Click here to Skip to main content
15,885,546 members
Home / Discussions / Web Development
   

Web Development

 
GeneralRe: SVG in IE8 Pin
ThetaClear29-Oct-13 21:47
ThetaClear29-Oct-13 21:47 
AnswerRe: SVG in IE8 Pin
Paulo Augusto Kunzel4-Nov-13 3:43
professionalPaulo Augusto Kunzel4-Nov-13 3:43 
GeneralRe: SVG in IE8 Pin
ThetaClear5-Nov-13 10:08
ThetaClear5-Nov-13 10:08 
GeneralRe: SVG in IE8 Pin
Paulo Augusto Kunzel5-Nov-13 11:47
professionalPaulo Augusto Kunzel5-Nov-13 11:47 
Question(HTML5 + Javascript + javascript Libs) or Razor Engine Pin
OmniSource28-Oct-13 22:48
OmniSource28-Oct-13 22:48 
Questionhow to password reminder Pin
Jassim Rahma28-Oct-13 11:00
Jassim Rahma28-Oct-13 11:00 
AnswerRe: how to password reminder Pin
Richard Deeming28-Oct-13 12:33
mveRichard Deeming28-Oct-13 12:33 
QuestionASP Net WebAPI Newby Question Pin
Kevin Marois28-Oct-13 6:14
professionalKevin 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
Jassim Rahma27-Oct-13 23:49 
SuggestionRe: get $_GET["id"] in window.location in PHP Pin
Richard MacCutchan28-Oct-13 2:09
mveRichard MacCutchan28-Oct-13 2:09 
AnswerRe: get $_GET["id"] in window.location in PHP Pin
Killzone DeathMan9-Jan-14 1:11
Killzone DeathMan9-Jan-14 1:11 
Questionwhy getting Strict Standards: Only variables should be passed by reference? Pin
Jassim Rahma27-Oct-13 23:20
Jassim Rahma27-Oct-13 23:20 
Questionexpire login's session Pin
Jassim Rahma27-Oct-13 0:06
Jassim Rahma27-Oct-13 0:06 
QuestionRe: expire login's session Pin
Richard MacCutchan27-Oct-13 2:07
mveRichard MacCutchan27-Oct-13 2:07 
SuggestionRe: expire login's session Pin
thatraja28-Oct-13 1:02
professionalthatraja28-Oct-13 1:02 
Questionprofile address url validation Pin
Jassim Rahma26-Oct-13 11:05
Jassim Rahma26-Oct-13 11:05 
AnswerRe: profile address url validation Pin
Thomas Daniels26-Oct-13 23:43
mentorThomas Daniels26-Oct-13 23:43 
GeneralRe: profile address url validation Pin
Jassim Rahma26-Oct-13 23:56
Jassim Rahma26-Oct-13 23:56 
AnswerRe: profile address url validation Pin
Thomas Daniels27-Oct-13 5:39
mentorThomas Daniels27-Oct-13 5:39 
AnswerRe: profile address url validation Pin
Bernhard Hiller27-Oct-13 23:24
Bernhard Hiller27-Oct-13 23:24 
QuestionNetwork for web hosting Pin
is516726-Oct-13 8:24
is516726-Oct-13 8:24 
Questionmake <select> NOT required if checkbox is checked Pin
Jassim Rahma26-Oct-13 2:10
Jassim Rahma26-Oct-13 2:10 
QuestionYears in Descending Order Pin
Jassim Rahma26-Oct-13 0:53
Jassim Rahma26-Oct-13 0:53 
AnswerRe: Years in Descending Order Pin
Richard MacCutchan26-Oct-13 21:42
mveRichard MacCutchan26-Oct-13 21:42 
GeneralRe: Years in Descending Order Pin
Jassim Rahma26-Oct-13 22:54
Jassim 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.