mat*_*att 2 asp.net-mvc-routing asp.net-web-api
我有一个有两个模型的网络项目 - IndicatorModel和GranteeModel.我也有相应的ApiControllers - IndicatorsController,和GranteesController.我打算将这个设置用于我的实际web项目的数据API,所以我在我的项目中创建了一个名为"Api"的新区域.在我的ApiAreaRegistration班上,我正在为这些控制器注册路由,如下所示:
context.Routes.MapHttpRoute(
name: "ApiDefault",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
基本上,http://myapp/api/indicators/123应该转到Indicators控制器的请求,它应该特别由接受整数参数的action方法处理.我的控制器类设置如下,它完美地工作:
public class IndicatorsController : ApiController
{
// get: /api/indicators/{id}
public IndicatorModel Get(int id)
{
Indicator indicator = ...// find indicator by id
if (indicator == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return new IndicatorModel(indicator);
}
}
Run Code Online (Sandbox Code Playgroud)
我的GranteesController课程设置相同:
public class GranteesController : ApiController
{
// get: /api/grantees/{id}
public GranteeModel Get(int granteeId)
{
Grantee grantee = ... // find grantee by Id
if (grantee == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return new GranteeModel(grantee);
}
}
Run Code Online (Sandbox Code Playgroud)
现在问题 - 如果我尝试请求http://myapp/api/grantees/123,我得到404,我100%肯定404 不是来自我的Get方法.首先,我已尝试在该方法中进行调试和记录,并且该方法实际上从未被命中.此外,请求的实际输出(json)如下所示:
{
"Message": "No HTTP resource was found that matches the request URI 'http://myapp/api/grantees/25'.",
"MessageDetail": "No action was found on the controller 'Grantees' that matches the request."
}
Run Code Online (Sandbox Code Playgroud)
此外,我的TraceWriter日志的输出如下所示:
;;http://myapp/api/grantees/10
DefaultHttpControllerSelector;SelectController;Route='controller:grantees,id:10'
DefaultHttpControllerSelector;SelectController;Grantees
HttpControllerDescriptor;CreateController;
DefaultHttpControllerActivator;Create;
DefaultHttpControllerActivator;Create;MyApp.Areas.Api.Controllers.GranteesController
HttpControllerDescriptor;CreateController;MyApp.Areas.Api.Controllers.GranteesController
GranteesController;ExecuteAsync;
ApiControllerActionSelector;SelectAction;
DefaultContentNegotiator;Negotiate;Type='HttpError', formatters=[JsonMediaTypeFormatterTracer...
Run Code Online (Sandbox Code Playgroud)
所以我的请求被正确路由 - 选择了正确的控制器,并正确设置了Id属性(10).但是,ApiControllerActionSelector没有在控制器上找到匹配的方法.我也尝试将[HttpGet]属性添加到我的Get方法中,但没有成功.
有没有人对这里可能发生的事情有任何想法?我不能为我的生活弄清楚为什么动作选择器没有找到正确的动作.
| 归档时间: |
|
| 查看次数: |
3880 次 |
| 最近记录: |