dda*_*ics 5 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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后有一个单独的控制器类,它位于同一个项目中,并继承自ApiController类.
using System.Web.Http;
namespace EP.Server
{
class UsersController : ApiController
{
[HttpGet]
public string Get()
{
return "Hello I'm a user...";
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我从任何浏览器调用服务时,错误消息都是这样的.
<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>
Run Code Online (Sandbox Code Playgroud)
有人知道那里有什么问题吗?不幸的是,我在网上找不到任何类似的问题/问题.
谢谢你到目前为止!
| 归档时间: |
|
| 查看次数: |
2778 次 |
| 最近记录: |