[英]routing for asp.net web api along with odata
My asp.net web application is hybrid which can have all different types of controllers - 我的asp.net Web应用程序是混合的,可以拥有所有不同类型的控制器 -
System.Web.Mvc.Controller
) System.Web.Mvc.Controller
) System.Web.Http.ApiController
) and System.Web.Http.ApiController
)和 System.Web.Http.OData.ODataController
) System.Web.Http.OData.ODataController
) I am trying to configure routing in WebApiConfig.cs
for Web API and OData controllers and it looks something like this - 我正在尝试在
WebApiConfig.cs
为Web API和OData控制器配置路由,它看起来像这样 -
public static void Register(HttpConfiguration config)
{
config.Routes.MapODataRoute("OData", "odata", CreateEdmModel());
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional });
}
public static IEdmModel CreateEdmModel()
{
ODataConventionModelBuilder modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<Detail>("Details");
return modelBuilder.GetEdmModel();
}
routes that work - 有效的路线 -
ASP.Net Web API
based ValuesController
with GetSummary
aciton method) ASP.Net Web API
的ValuesController
和GetSummary
aciton方法) HomeController
) HomeController
) routes that doesnt work- 不起作用的路线 -
My regular MVC controllers execute fine, Web API controllers also work fine, but somehow routing is not working for OData controllers. 我的常规MVC控制器执行良好,Web API控制器也工作正常,但不知何故路由不适用于OData控制器。 Has anyone tried mix and match in a single application and able to make it work?
有没有人尝试在一个应用程序中混合搭配并能够使其工作? I will also need to specify action method name in the route, since all action method are mostly
GET
since it is a reporting application. 我还需要在路由中指定操作方法名称,因为所有操作方法都是
GET
因为它是一个报告应用程序。
Yes, it is possible to mix all these controllers. 是的,可以混合所有这些控制器。 If MVC's routing was activated first in Global.asax.cs then MVC's default route may not allow to reach OData controllers.
如果首先在Global.asax.cs中激活MVC的路由,则MVC的默认路由可能不允许到达OData控制器。 Changing the order of lines in Global.asax.cs as shown below will solve the issue.
如下所示更改Global.asax.cs中的行顺序将解决该问题。 MVC controllers routing is usually setup in RouteConfig.cs , Web API and OData routing in WebAPIConfig.cs if project templates were used.
如果使用项目模板,MVC控制器路由通常在WebAPIConfig.cs中的RouteConfig.cs,Web API和OData路由中设置。
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
GlobalConfiguration.Configure(WebApiConfig.Register); // moved up before MVC's setup
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.