0

I have Angular application that is consuming .NET CORE 2.0 Web API, I need to ensure route define strongly type of class 'ResponseCurrentStateDataView' and receive JSON object as parameter from Angular application

Angular Class structure

export class ResponseCurrentStateDataModel
{
  consultationId:string;
  responseTypeId: string;
  responseTypeTitle:string;
  responsesRequestedStatus:string;
}

C# class structure

public class ResponseCurrentStateDataView
{
    public Guid ConsultationId { get; set; }
    public Guid ResponseTypeId { get; set; }
    public string ResponseTypeTitle { get; set; }
    public string ResponsesRequestedStatus { get; set; }
}

Web API (how to define route structure and parameter??? )

    [Route("[action]/{responseCurrentStateObject}")]   // i believe this is incorrect
    [HttpGet]
    public JsonResult GetResponsesByResponseStatusType(ResponseCurrentStateDataView responseCurrentStateObj)  ??????
    {
        //my code.... responseList
        return Json(responsesList);

    }
1
  • if you want to pass an object or JSON to an web api call, use POST or PUT method. GET accepts only an ID and returns the object. [FromBody] is used to get the JSON body passed from UI to API call Commented Apr 5, 2018 at 14:43

1 Answer 1

1

So you really shouldn't be passing json text though a GET request. If you try using a POST you can do it like so:

[Route("api/[controller]")]
public class ValuesController : Controller
{

    // POST api/values
    [HttpPost]
    public TestObjectFromNg Post([FromBody] TestObjectFromNg value)
    {
        return value;
    }
}

public class TestObjectFromNg
{
    public int Id { get; set; }
    public string Value { get; set; }
}

Also notice I typed the return value and I simply returned the typed value with out calling Json(). Web Api 2 will convert the object to json automagically.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.