当我升级到Odata v4时, 现在在v3中运行的所有操作都会找不到404错误:
[HttpException]: The controller for path /odata/MyModel/CheckStatus was not found or does not implement IController.
at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
我的代码如下:
WebApiConfig:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
...
config.MapODataServiceRoute(
routeName: "OData",
routePrefix: "odata",
model: GetModel(),
batchHandler: new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));
config.AddODataQueryFilter();
GetModel:
builder.EntityType<MyModel>().Collection.
Action("CheckStatus").Parameter<IEnumerable<EntityStatus<MyModel>>>("modelStatus");
...
ODataController:
public async Task<IHttpActionResult> CheckStatus([FromBody] IEnumerable<EntityStatus<MyModel>> modelStatus)
...
其中EntityStatus是一个通用类,如下所示:
public class EntityStatus<T> where T : class
{
[Key, Column(Order = 0)]
public string Status { get; set; }
[Key, Column(Order = 1)]
public string Original { get; set; }
public T Model { get; set; }
}
在Ajax调用中:
var url = "/odata/MyModel/CheckStatus";
$.ajax({
url: url,
...
我尝试添加命名空间,修改 web.config 并使用OdataRoute
但没有成功。你能帮助我绕过上面显示的这些错误吗?
答案 0 :(得分:1)
获取所有OData 4动作&#34; 404未找到&#34;错误只是用以下内容替换Web.config中的modules部分:
<modules runAllManagedModulesForAllRequests="true"></modules>
我在某处读到这不是一个好的解决方案。幸运的是,它现在正在运作。
如果您与我分享您对此解决方案的经验,我会很高兴。