【发布时间】:2013-03-07 20:54:30
【问题描述】:
我最近刚刚在我的 ASP.NET MVC4 项目中添加了一个名为 ServerInfoController 的新 ApiController(我已经在 Controllers 文件夹中添加了一个 ApiController)。我使用 CRUD 模板选项创建了这个;这是它的样子:
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebFrontend.Models;
namespace WebFrontend.Controllers
{
public class ServerInfoController : ApiController
{
// GET api/serverinfo
public Dictionary<string, string> Get()
{
var settings = GlobalStaticVars.StaticCore.LoadServerSettings("Last", "Server");
if (settings != null)
{
return new Dictionary<string, string>
{
{"ServerIP", settings.Address},
{"ServerPort", settings.Port.ToString()}
};
}
return new Dictionary<string, string>
{
{"ServerIP", ""},
{"ServerPort", ""}
};
}
}
}
但是,每次我尝试在浏览器中导航到该资源时,都会收到 404 错误。我知道我的路由已正确注册,因为我的其他控制器仍然可以在 /api 端点访问。
以下是WebApiConfig.cs中配置的路由(默认路由):
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
【问题讨论】:
-
你能显示你的 web api 路线吗?还有你打电话的网址是什么。
-
完成。我在上面添加了它。此外,我调用的 URL 类似于 localhost:66555/api/serverinfo
标签: c# asp.net-mvc-4 asp.net-web-api