【问题标题】:Cannot run a restfull server in a ASP.Net without MVC在没有 MVC 的情况下无法在 ASP.Net 中运行安静的服务器
【发布时间】:2014-07-16 14:44:08
【问题描述】:

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

这是我的课:

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

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

 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# 表单的客户端:

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

但是当我运行客户端并单击按钮时,我收到此错误:

System.AggregateException:发生一个或多个错误。

System.Net.Sockets.SocketException:无法建立连接 因为目标机器主动拒绝了 127.0.0.1:8080

【问题讨论】:

标签: c# asp.net-web-api


【解决方案1】:

可能有一个或多个原因。

1) 确保 8080 端口没有被其他应用程序占用,尝试在不同的端口上运行。

试试client.BaseAddress = new Uri("http://localhost:9090/");

2) 默认情况下,侦听特定 HTTP 地址需要管理员权限。因此,当您运行应用程序时,您可能会收到错误消息:“HTTP 无法注册 URL http://+:8080/”。为避免此错误,请以提升的管理员权限运行 Visual Studio。

3) 确保两个项目并行运行。如果不这样做。

转到解决方案属性->通用属性->启动项目并选择多个启动项目

【讨论】:

  • 通过使用不同的端口我得到这个错误:对象引用未设置为对象的实例。我一直在以管理员身份运行 Visual Studio
猜你喜欢
  • 2013-03-09
  • 1970-01-01
  • 2012-04-15
  • 2016-06-22
  • 2012-06-19
  • 2018-08-29
  • 2019-02-23
  • 1970-01-01
  • 2016-04-12
相关资源
最近更新 更多