【发布时间】:2015-12-09 15:41:43
【问题描述】:
我正在使用 Web Api 2、C# 和 .NET Framework 4.5.1 开发一个 ASP.NET MVC 网站。
我正在尝试进行版本控制,但我做错了。
我在新命名空间中创建了新版本的控制器:
namespace MyProject.Web.API.Controllers.v2
{
[AllowAnonymous]
[ActionLogFilter]
public class ExternalCodesController : ApiController
{
public ExternalCodesController()
{
}
[HttpGet]
[Route("api/v2/ExternalCodes")]
public HttpResponseMessage Get()
{
[ ... ]
}
[HttpGet]
[Route("api/v2/ExternalCodes")]
public HttpResponseMessage Get(
byte? codeLevel,
int batchId,
int? lineId,
int productId,
string startingCode,
int? quantity)
{
[ ... ]
}
}
}
但是,当我使用这个 URI:http://myHost:53827/api/v2/ExternalCodes?codeLevel=&batchId=5&lineId=&productId=7&startingCode=&quantity= 执行 GET 时,我得到一个 NotFound HTTP 状态代码。但我也用相同的状态码测试了这个 URI:http://myHost:53827/api/v2/ExternalCodes。
我做错了什么?
我的WebApiConfig.cs 是:
namespace MyProject.Web.API
{
/// <summary>
/// Class to config Web API routes and filters.
/// </summary>
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Json configuration
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
// Remove formatting to make json smaller.
json.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.None;
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// Filters.
config.Filters.Add(new ExceptionFilter());
config.Filters.Add(new UnhandledExceptionFilter());
}
}
}
【问题讨论】:
-
你的路由配置是什么样的?你有默认路由(以及属性路由)
-
@Alex 我已经用 WebApiConfig 文件编辑了我的问题。
-
尝试注释掉“DefaultApi”路由注册(基于约定的路由)...只是想看看是否有效
-
不,它不起作用。
-
@VansFannel 我将您的类添加到我拥有的现有项目中,编译,然后使用提供的 url 调用操作,仅更改端口号。动作方法被调用!我不知道你有什么问题,但我不相信它在你的控制器或你的路由属性中。我确实使用了 uri localhost:6137/api/v2/…
标签: c# asp.net asp.net-web-api asp.net-web-api2