c# - ASP.NET WebAPI Selfhost 没有解析到 Controller 的路由

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

我正在玩“5.0.0-rc1”版本的“Microsoft.AspNet.WebApi.SelfHost”NuGet 包(也尝试了当前的稳定版)但不幸的是我无法让我的应用程序设置来解析已配置的路由到我的 ApiController 实现。

该应用程序非常简单,几乎在所有示例中都能找到。它包含一个 .NET 控制台应用程序,自托管服务位于主类中。

using System.Web.Http.SelfHost;
using System.Web.Http;
using System;
namespace EP.Server
{
class Program
{
    static void Main(string[] args)
    {
        var config = new HttpSelfHostConfiguration("http://localhost:60064");
        config.Routes.MapHttpRoute(
            name: "Default Route",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        using (var server = new HttpSelfHostServer(config))
        {
            server.OpenAsync().Wait();
            Console.WriteLine("Server ist running on "+config.BaseAddress + " hit ENTER to stop.");
            Console.ReadLine();
            server.CloseAsync().Wait();
        }
    }
}
}

然后有一个单独的 Controller 类,它位于同一个项目中并继承自 ApiController 类。

using System.Web.Http;

namespace EP.Server
{
class UsersController : ApiController 
{
    [HttpGet]
    public string Get()
    {
        return "Hello I'm a user...";
    }
}
}

当我从任何浏览器调用该服务时,错误消息听起来像这样。

<Error>
<Message>No HTTP resource was found that matches the request URI   'http://localhost:60064/api/Users'.</Message>
<MessageDetail>No type was found that matches the controller named 'Users'.</MessageDetail>
</Error>

有人知道那里出了什么问题吗?不幸的是,我在网上找不到任何类似的问题。

谢谢你!

最佳答案

Web API Controller 需要公共(public)

关于c# - ASP.NET WebAPI Selfhost 没有解析到 Controller 的路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18952740/

相关文章:

c# - 从属于一个对象的所有列表中获取元素总数

c# - WPF Datagrid 列格式编号以包含逗号

javascript - 在asp.net中开发extjs枢轴网格的步骤

c# - Web.config 设置无法接受超过 1 GB 的文件大小

c# - RSA加密C#

c# - 没有网络时 ClickOnce 初始化失败

asp.net - App_Code 和 web.config

c# - 包管理器控制台如何知道要使用哪个连接字符串?

asp.net-mvc - 为什么 MVC Post (ViewModel) 不会使用异步返回更新后的表单?

asp.net-mvc - 从返回具有可变列的表的 Entity Framework 调用存储过程