1

So I have this code and it should return the list of items but I can't get it to work. I know there is a mismatch between HttpResponseMessage and List<string> but I am not able to convert it to return.

image

I know that it's because of the HttpResponseMessage type and list don't match but I don't know how to convert it

namespace NovaWebApi.Controllers
{
    public class TermsController : WebApiBase
    {
        [HttpGet]
        public HttpResponseMessage GetTermsUrl()
        {
            List<string> terms = new List<string>();
            terms.Add("https://www.nbg.gov.ge/index.php?m=2");
            terms.Add("https://www.fms.gov.ge/");

            return terms;
        }
    }

}
3
  • 1
    The message it right there in that picture telling you what is wrong. Commented Jul 28, 2020 at 11:43
  • return type should be of HttpResponseMessage type. stackoverflow.com/questions/12240713/… Commented Jul 28, 2020 at 11:43
  • If none of these answers provide a solution you can accept as an answer for you please edit your question to indicate why your requirement is not met by them with additional details. Commented Jul 29, 2020 at 13:01

3 Answers 3

2

Your HttpGet method returns HttpResponseMessage.Try this one:

[HttpGet]

    public List<string> GetTermsUrl()
    {
        List<string> terms = new List<string>();
        terms.Add("https://www.nbg.gov.ge/index.php?m=2");
        terms.Add("https://www.fms.gov.ge/");

        return terms;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I did that and it worked just fine sorry for the late reply. I haven't tried other ones this one worked for me.
1

You are trying to return List<string> as HttpResponseMessage which is not possible.Try this:

public class TermsController : Controller // this part to return Ok() without error.

Then:

return Ok(terms);

5 Comments

I've tried it but "name Ok doesn't exist in the current contexts"
Any intellisense suggestions?
Just to generate a new Ok method
It did not help sorry. It messed up [httpget] and I changed it to public class TermsController : WebApiBase that we use in different controllers but I still get an error
Not WebApiBase, you should use Controller to inherit.
1

Since you indicate you wish to return HttpResponseMessage type, you have to create one that includes your list.

Web API will create a serialized version in the response body using a formatter for the model, in this case your list; writing the serialized model into the response body.

[HttpGet]
public HttpResponseMessage GetTermsUrl()
{
     // Get a list of products from a database.
     List<string> terms = new List<string>();
     terms.Add("https://www.nbg.gov.ge/index.php?m=2");
     terms.Add("https://www.fms.gov.ge/");
     // Write the list to the response body.
     HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, terms);
     return response;
)

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.