C# 没有MVC,无法在ASP.Net中运行restfull服务器
我正在尝试在没有mvc的简单asp.net应用程序中运行简单的restfull服务器(遵循本教程: (这将进入一个在线门户) 这是我的班级:C# 没有MVC,无法在ASP.Net中运行restfull服务器,c#,asp.net-web-api,C#,Asp.net Web Api,我正在尝试在没有mvc的简单asp.net应用程序中运行简单的restfull服务器(遵循本教程: (这将进入一个在线门户) 这是我的班级: public class Foods { public string FoodName { get; set; } public string Price { get; set; } public string Type { get; set; } public string Content { get; set; } public Foods() { }
public class Foods
{
public string FoodName { get; set; }
public string Price { get; set; }
public string Type { get; set; }
public string Content { get; set; }
public Foods()
{
}
}
这是我的控制器:
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;
}
}
当我运行它时,没有错误,空白页显示为
这是一个客户端,它是一个简单的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();
}
}
private void按钮1\u单击(对象发送者,事件参数e)
{
HttpClient=新的HttpClient();
client.BaseAddress=新Uri(“http://localhost:8080/");
client.DefaultRequestHeaders.Accept.Add(
新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
尝试
{
HttpResponseMessage response=client.GetAsync(“api/foods”).Result;
if(响应。IsSuccessStatusCode)
{
//解析响应体。阻塞!
var foods=response.Content.ReadAsAsync().Result;
foreach(食品中的食品)
{
字符串foodinfo=food.FoodName+“”+food.Content+“”+food.Type+“”+food.Price;
listBox1.Items.Add(foodinfo);
}
}
}
捕获(例外情况除外)
{
textBox1.Text=ex.ToString();
}
}
但是,当我运行客户端并单击按钮时,会出现以下错误:
System.AggregateException:发生一个或多个错误
System.Net.Sockets.SocketException:无法建立连接
因为目标机器主动拒绝它127.0.0.1:8080
这可能有一个或多个原因 1) 确保端口8080未被其他应用程序占用,请尝试在其他端口上运行 请尝试
client.BaseAddress=新Uri(“http://localhost:9090/");代码>
2) 默认情况下,侦听特定HTTP地址需要管理员权限。因此,运行应用程序时,可能会出现错误:“HTTP无法注册URLHTTP://+:8080/
”。若要避免此错误,请使用提升的管理员权限运行Visual Studio
3) 确保两个项目并行运行。如果没有,就去做
转到解决方案属性->公共属性->启动项目,并使用不同的端口选择多个启动项目,我收到以下错误:对象引用未设置为对象的实例。我一直在以管理员身份运行visual studio受保护,因此问题可能会给您一些想法:。
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();
}
}