0

My task is to create services (using asp.net api) and use it for android aplication.

I have never done anything like this so I have a problems at begining. :(

First I created Class Library with few classes. Second I created asp.net web application and refered class library into it.

My problem is that I dont know how to access to methode from controller.

Here comes code:

Controller class:

  public class ValuesController : ApiController
{
    // GET api/<controller>
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/<controller>/5
    public string Get(int id)
    {
        return "value";
    }

    // POST api/<controller>
    public void Post([FromBody]string value)
    {
    }

    // PUT api/<controller>/5
    public void Put(int id, [FromBody]string value)
    {
    }

    // DELETE api/<controller>/5
    public void Delete(int id)
    {


    }
    public List<BuzzMonitor.Web.Message> Search()
    {
        MessageHandler mh = new MessageHandler();

        List<BuzzMonitor.Web.Message> messages = null;

        messages = mh.Search(string text, DateTime dateFrom, DateTime dateTo, List<int> themeIds, List<int> sourceIds);

        return messages;
    }


}

MessageHandler class:

 public class MessageHandler
{
    private List<Message> _dummyMessages = new List<Message>()
        {
            new Message(){
                MessageId = 1,
                CreatedDate = new DateTime(2014, 5, 27),
                Text = "Srpska vodoprivreda...",
                Autor = "Marko Markovic",
                Source = "Twitter"

            },
            new Message(){
                MessageId = 2,
                CreatedDate = new DateTime(2014, 5, 27),
                Text = "Aerodrom Beograd...",
                Autor = "Zoran Zoric",
                Source = "B92"

            }
        };

    public List<Message> GetLatestMessages(int nrMessagesToReturn)
    {
        List<Message> retVal;

        retVal = this._dummyMessages.GetRange(0, nrMessagesToReturn);

        return retVal;
    }

    public List<Message> Search(string text, DateTime dateFrom, DateTime dateTo, List<int> themeIds, List<int> sourceIds, int pageIndex, int pageSize)
    {
        List<Message> retVal;

        retVal = this.Search(text, dateFrom, dateTo, themeIds, sourceIds);

        if (retVal != null && retVal.Count > 0)
        {
            retVal = retVal.GetRange(pageIndex * pageSize, pageSize);
        }

        return retVal;
    }

    public List<Message> Search(string text, DateTime dateFrom, DateTime dateTo, List<int> themeIds, List<int> sourceIds)
    {
        List<Message> retVal = null;

        retVal = this._dummyMessages.Where(m => m.Text.IndexOf(text, StringComparison.OrdinalIgnoreCase) > -1 &&
            m.CreatedDate >= dateFrom &&
            m.CreatedDate < dateTo &&
            (themeIds == null || themeIds.Count == 0 || (themeIds.Contains(m.ThemeId))) &&
            (sourceIds == null || sourceIds.Count == 0 || (sourceIds.Contains(m.SourceId)))).ToList<Message>();

        return retVal;
    }


}

Message class:

    public class Message
{
    public int MessageId { get; set; }

    public DateTime CreatedDate { get; set; }

    public string Text { get; set; }

    public string Autor { get; set; }

    public string Source { get; set; }

    public int ThemeId { get; set; }

    public int SourceId { get; set; }
}

Ok, one more, I want to access MessageHandler class and it's methode Search from controller, but I dont know how, because when I type MessageHandler and put dot I cant type anything more :(

Thank you all for your help!

7
  • 1
    I don't understand your question, you should just create an instance of MessageHandler and then call the methods on the instance Commented May 28, 2014 at 11:54
  • @3dd ok I did it. I edited controller class you can see it, but still all parametars of the method are underlined with red as misteke :( do you know why? Commented May 28, 2014 at 12:08
  • Which parameters are underlined and what's the error message you get? Commented May 28, 2014 at 12:13
  • mh.Search(string text, DateTime dateFrom, DateTime dateTo, List<int> themeIds, List<int> sourceIds); - that's not how you call a method. You need to pass values instead of parameter declarations. You need to follow some basic C# tutorials. Commented May 28, 2014 at 12:13
  • 1
    I'm noticing that you're posting a lot of questions on StackOverflow about this one snippet of code - why don't you try getting a beginner C# book or searching for some tutorials online? That way you will actually understand what you're doing. Commented May 28, 2014 at 12:17

1 Answer 1

1

Your issue is you are indicating your argument types as you are calling your method. You only indicate the type when you define your method:

messages = mh.Search(string text, DateTime dateFrom, DateTime dateTo, List<int> themeIds, List<int> sourceIds);

should be:

messages = mh.Search(text, dateFrom, dateTo, themeIds, sourceIds);

However, you still have anpother issue in that those variables don't exist. Where are they coming from? I presume you want to pass them into the Web API action method.

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.