Click here to Skip to main content
16,020,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to run a simple restfull server in a simple asp.net application without the mvc(following this tutorial : http://www.codeproject.com/Articles/769671/Web-API-without-MVC (which will trun into a online portal).

here is my class :

C#
public class Foods
{
public string FoodName { get; set; }
public string Price { get; set; }
public string Type { get; set; }
public string Content { get; set; }
}


and here is my controller:

C#
public class FoodController : ApiController
{
public List<Foods> _productList;

public List<Foods> GetProductList()
{
    _productList = new List<Foods>{
       new Foods{FoodName= "pizza",Content= "bread cheese",Type="Main",Price="100"},
       new Foods{FoodName= "rice",Content= "rice and ...",Type="Main",Price="100"}
    };
    return _productList;
}
 }

and here is my asp.net page code(it is simple nothing to show):

C#
public partial class _Default : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
  {

    var config = new HttpSelfHostConfiguration("http://localhost:8080/");
    config.Routes.MapHttpRoute(
        "API Default", "api/{controller}/{id}",
        new { id = System.Web.Http.RouteParameter.Optional });

    using (HttpSelfHostServer server = new HttpSelfHostServer(config))
    {
        server.OpenAsync().Wait();
    }
}
}


when i run it there is no error and the blank page is shown

and here is the client which is a simple c# form with a list box and a button:

C#
private void button1_Click(object sender, EventArgs e)
   {
       HttpClient client = new HttpClient();
       client.BaseAddress = new Uri("http://localhost:8080/");
       client.DefaultRequestHeaders.Accept.Add(
          new MediaTypeWithQualityHeaderValue("application/json"));
       try
       {
           HttpResponseMessage response = client.GetAsync("api/foods").Result;
           if (response.IsSuccessStatusCode)
           {
               // Parse the response body. Blocking!
               var foods = response.Content.ReadAsAsync<IEnumerable<Foods>>().Result;
               foreach (Foods food in foods)
               {
                   string foodinfo = food.FoodName + "   " + food.Content + "    " + food.Type + "    " + food.Price;
                   listBox1.Items.Add(foodinfo);
               }
           }
       }
       catch (Exception ex)
       {
           textBox1.Text = ex.ToString();
       }
   }


but when i run the client and click the button i get this error:
Object reference not set to an instance of an object.
and the page i saying source is not available
Posted
Comments
Kornfeld Eliyahu Peter 16-Jul-14 6:59am    
On which line the error is?
Saeedek 16-Jul-14 7:02am    
i don't know and that is the problem.it is like this when it gives me the error:
http://upload7.ir/imgs/2014-07/13027254904301851632.jpg

i followed this tutorial : http://www.codeproject.com/Articles/769671/Web-API-without-MVC
Kornfeld Eliyahu Peter 16-Jul-14 10:08am    
Go and debug you code line-by-line. That will give you the exact spot where the error is...
Saeedek 16-Jul-14 10:12am    
did you see the screenshot of my error? i have done that.i have put a breakpoint on every line but after i click the button i opens up a screen which is saying no source available and the exception

1 solution

Quote:
but when i run the client and click the button i get this error:
Object reference not set to an instance of an object.
What does Object reference not set to an instance of an object[^]
Why do I get the error "Object reference not set to an instance of an object"?[^]
Quote:
i don't know and that is the problem.it is like this when it gives me the error:
http://upload7.ir/imgs/2014-07/13027254904301851632.jpg
That link not working for me. Always use sites like dropbox or imgur which is nice & receommended by CP members.
Quote:
did you see the screenshot of my error? i have done that.i have put a breakpoint on every line but after i click the button i opens up a screen which is saying no source available and the exception
Getting rid of “There is no source code available for the current location.”[^]

Go ahead.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900