没有MVC,无法在ASP.Net中运行restfull服务器 [英] Cannot run a restfull server in a ASP.Net without MVC

查看:117
本文介绍了没有MVC,无法在ASP.Net中运行restfull服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在没有mvc的简单asp.net应用程序中运行一个简单的restfull服务器(遵循本教程:http://www.codeproject.com/Articles/769671/Web-API-without-MVC(这将是一个在线门户网站。



这是我的班级:



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 :

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:

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;
}
 }



这里是我的asp.net页面代码(很简单没什么好显示的):




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

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();
    }
}
}





当我运行它时没有错误和空白页显示



这里是客户端,它是一个简单的c#表单,带有一个列表框和一个按钮:





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:

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

推荐答案

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"?[^]

引用:

我不知道和这就是问题。当它给我错误时就是这样:

http://upload7.ir/imgs/2014-07/13027254904301851632.jpg

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

该链接不适合我。总是使用像dropbox或imgur这样的网站,这是很好的和由CP成员推荐。

That link not working for me. Always use sites like dropbox or imgur which is nice & receommended by CP members.

引用:

你看到了我的错误截图吗?我已经完成了。我已经在每一行都设置了一个断点但是在我点击按钮后我打开了一个屏幕,上面写着无源可用和异常

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.


这篇关于没有MVC,无法在ASP.Net中运行restfull服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆