我有一个 MVC4 应用程序,我创建了一个基于 webapi 的测试控制器,如下所示:
public class User1
{
public int Id { get; set; }
public string name { get; set; }
public User1(int id, string name)
{
this.Id = id;
this.name = name;
}
}
public class Test1Controller : ApiController
{
public User1 Get(int id)
{
return new User1(id, "hello");
}
}
我注意到我有一个 webapiconfig 类:
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
我假设这将默认输出json结果正确吗?
当我去:
http://localhost:61146/api/test1/get/1
或者
http://localhost:61146/test1/get/1
我收到此错误:
The resource cannot be found.
这究竟是如何映射的,还是我必须将它放在一个特殊的文件夹中?我猜它会自己映射它,因为我是从 ApiController 继承的