Click here to Skip to main content
15,311,330 members
Home / Discussions / Web Development
   

Web Development

 
QuestionPHP4.3 Mod, coloring table rows Pin
jkirkerx1-Dec-20 9:29
professionaljkirkerx1-Dec-20 9:29 
AnswerRe: PHP4.3 Mod, coloring table rows [solved] Pin
jkirkerx1-Dec-20 9:34
professionaljkirkerx1-Dec-20 9:34 
AnswerRe: PHP4.3 Mod, coloring table rows Pin
Richard Deeming1-Dec-20 21:55
mveRichard Deeming1-Dec-20 21:55 
GeneralRe: PHP4.3 Mod, coloring table rows Pin
jkirkerx2-Dec-20 8:30
professionaljkirkerx2-Dec-20 8:30 
QuestionHow to insert roles in php and mysql? Pin
Nicolas Veloso16-Nov-20 6:04
MemberNicolas Veloso16-Nov-20 6:04 
SuggestionRe: How to insert roles in php and mysql? Pin
Richard Deeming16-Nov-20 21:51
mveRichard Deeming16-Nov-20 21:51 
QuestionBlazor SPA Pin
Super Lloyd16-Nov-20 1:26
MemberSuper Lloyd16-Nov-20 1:26 
QuestionDesign questions about asp.net core website and asp.net core webapi Pin
instantmaker13-Nov-20 9:04
Memberinstantmaker13-Nov-20 9:04 
AnswerRe: Design questions about asp.net core website and asp.net core webapi Pin
Afzaal Ahmad Zeeshan14-Nov-20 1:50
mveAfzaal Ahmad Zeeshan14-Nov-20 1:50 
GeneralRe: Design questions about asp.net core website and asp.net core webapi Pin
instantmaker14-Nov-20 2:49
Memberinstantmaker14-Nov-20 2:49 
GeneralRe: Design questions about asp.net core website and asp.net core webapi Pin
Afzaal Ahmad Zeeshan14-Nov-20 3:17
mveAfzaal Ahmad Zeeshan14-Nov-20 3:17 
GeneralRe: Design questions about asp.net core website and asp.net core webapi Pin
instantmaker16-Nov-20 13:03
Memberinstantmaker16-Nov-20 13:03 
AnswerRe: Design questions about asp.net core website and asp.net core webapi Pin
Leif Simon Goodwin22-Dec-20 7:17
MemberLeif Simon Goodwin22-Dec-20 7:17 
GeneralRe: Design questions about asp.net core website and asp.net core webapi Pin
instantmaker22-Dec-20 7:34
Memberinstantmaker22-Dec-20 7:34 
GeneralRe: Design questions about asp.net core website and asp.net core webapi Pin
Leif Simon Goodwin22-Dec-20 9:07
MemberLeif Simon Goodwin22-Dec-20 9:07 
GeneralRe: Design questions about asp.net core website and asp.net core webapi Pin
instantmaker22-Dec-20 10:39
Memberinstantmaker22-Dec-20 10:39 
QuestionDesign question with asp.net core and asp.net core api Pin
instantmaker13-Nov-20 6:18
Memberinstantmaker13-Nov-20 6:18 
QuestionI keep getting a “Call to undefined function..” error displaying Pin
IAmSirAskAlot9-Nov-20 18:25
MemberIAmSirAskAlot9-Nov-20 18:25 
QuestionWeb API Deserialization Problem Pin
Kevin Marois9-Nov-20 8:04
professionalKevin Marois9-Nov-20 8:04 
I'm working on a RestSharp wrapper class. I can call the API and step into the controller. The controller creates the response data and returns it. However, when I get the entity back from the API its properties are not set.

Controller
public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        config.Formatters.JsonFormatter.SupportedMediaTypes
            .Add(new MediaTypeHeaderValue("application/json"));

    }
}
public class CustomerController : ApiController
{
    [HttpGet]
    public APIResponse<CustomerEntity> GetCustomer(int id)
    {
        var response = new APIResponse<CustomerEntity>
        {
            Success = true
        };

        try
        {
            var data = new CustomerEntity
            {
                Id = 1,
                CustomerName = "ACME Widgets"
            };

            response.Result = data;
        }
        catch (Exception e)
        {
            var rm = new ReponseMessage
            {
                Exception = e,
                Message = e.Message
            };

            response.Messages.Add(rm);
        }

        return response;
    }
}

RestSharp Wrapper
public class APIExecutor
{
    #region Private Fields
    private RestClient client;

    private RestRequest request;

    private string baseURL;
    #endregion

    public APIExecutor(string url, Method method = Method.POST)
    {
        baseURL = "<a href="http://localhost:61037/api/">http://localhost:61037/api/</a>"; // Get from App.Config

        if (string.IsNullOrEmpty(url))
        {
            throw new ArgumentNullException("Url");
        }

        client = new RestClient(baseURL);

        client.AddHandler("application/json", () => { return new JsonDeserializer(); });

        request = new RestRequest(url, method)
        {
            RequestFormat = DataFormat.Json
        };
    }

    public async Task<APIResponse<T>> ExecuteAsync<T>()
    {
        // Execute the request
        IRestResponse<T> response = await client.ExecuteAsync<T>(request);

        // Create a response
        var results = new APIResponse<T>
        {
            StatusCode = response.StatusCode,
            RequestURL = response.ResponseUri.ToString()
        };

        // If the request succeeded... 
        if (response.StatusCode == HttpStatusCode.OK)
        {
            results.Success = true;
            results.Result = response.Data; // <====================================== ENTITY IS HERE BUT ITS PROPERTIES ARE NOT SET

            return results;
        }
        else
        {
            // Handle the error
            var exception = LogError(new Uri(baseURL), request, response);
            var message = new ReponseMessage
            {
                Message = response.StatusDescription,
                Exception = exception 
            };

            results.Messages.Add(message);
            results.Success = false;
        }

        return results;
    }
}

The response content does not look like JSON. I'm wondering if either the WebApiConfig or RestClient.AddHandler is wrong.

I could some expert eyes on this. Not sure why deserialization isn't working.

Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.


modified 9-Nov-20 14:20pm.

AnswerRe: Web API Deserialization Problem Pin
Richard Deeming9-Nov-20 22:20
mveRichard Deeming9-Nov-20 22:20 
GeneralRe: Web API Deserialization Problem Pin
Kevin Marois10-Nov-20 7:18
professionalKevin Marois10-Nov-20 7:18 
GeneralRe: Web API Deserialization Problem Pin
Richard Deeming10-Nov-20 21:41
mveRichard Deeming10-Nov-20 21:41 
GeneralRe: Web API Deserialization Problem Pin
Kevin Marois11-Nov-20 7:16
professionalKevin Marois11-Nov-20 7:16 
GeneralRe: Web API Deserialization Problem Pin
Kevin Marois11-Nov-20 7:54
professionalKevin Marois11-Nov-20 7:54 
GeneralRe: Web API Deserialization Problem Pin
Richard Deeming11-Nov-20 22:42
mveRichard Deeming11-Nov-20 22:42 

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.