【问题标题】:Configuring Odata with Web Api Areas使用 Web Api 区域配置 Odata
【发布时间】:2013-03-24 22:43:31
【问题描述】:

我有一个配置为将 Web Api 与区域一起使用的工作 Web 应用程序。我有 2 个区域,管理员和客户。这意味着,要定位控制器,我必须像这样调用 url:

/xxxWebClient/api/1.0.0/projectid/Products

一切正常。 这是我的路由配置:

//Route to Client
        routes.MapRoute(
           name: "Client",
           url: "Client",
           defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
           namespaces: new string[] { "xxx.Web.Areas.xxxWebClient.Controllers" }
       ).DataTokens.Add("Area", "xxxWebClient");

        //Route to Admin
        routes.MapRoute(
           name: "Admin",
           url: "Admin",
           defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
           namespaces: new string[] { "xxx.Web.Areas.xxxWebAdmin.MvcControllers" }
       ).DataTokens.Add("Area", "xxxWebAdmin");

我现在想启用 odata 以便它可以针对同一个控制器。问题是我不仅想使用 Odata WebAPI 功能,只需将 [Queryable] 属性添加到方法中就可以使用这些功能(并且效果很好),而且我必须将我的 Controller 扩展到新的 AsyncEntitySetController 以便检索并使用调用 odata url 时自动生成的 QueryOptions。 我已经有了最新的 nuGet 包(odata、edm 和 spatial),为了编译项目,我必须将这些程序集绑定重定向添加到 web.config:

<dependentAssembly>
    <assemblyIdentity name="Microsoft.Data.OData" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.4.0.0" newVersion="5.4.0.0" />
  </dependentAssembly>      
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Data.Edm" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.4.0.0" newVersion="5.4.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Spatial" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.4.0.0" newVersion="5.4.0.0" />
  </dependentAssembly>

然后我在 Global.asax 中添加了这段代码(它现在在 Application_BeginRequest() 中,以便在每次页面加载时都通过,然后它将在 Application_Start 方法中):

ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
            modelBuilder.EntitySet<Product>("Products");

            Microsoft.Data.Edm.IEdmModel model = modelBuilder.GetEdmModel();
            try { 
                GlobalConfiguration.Configuration.Routes.MapODataRoute("ODataRoute", "xxxWebClient/api/1.0.0/projectid", model); 
            }

关键是当我定位我的 odata 控制器 url 时,我得到一个 406(不可接受的)状态代码。 (例如'xxxWebClient/api/1.0.0/projectid/Products')。

我的控制器的定义是这样的:

public class ProductsController : AsyncEntitySetController<Product, string>
{

我已经在一个空白测试项目上成功测试了这个 AsyncEntitySetController,我希望能够将它与区域集成。任何想法都非常感谢。谢谢。

【问题讨论】:

    标签: controller entity odata areas


    【解决方案1】:

    看来,根据我在这个主题上找到的唯一资源,我想要完成的事情还不可能。请参阅 GrahameHorner 的评论及其回复 HERE。 无论如何,我找到了解决我的问题的方法,即不使用 AsyncEntitySetController,而是像以前一样使用 ApiController,将 ODataQueryOptions 作为控制器的参数传递,因此 WebAPI 会自动解析它们。然后我有了 C# ODataQueryOptions 对象,我可以将其应用于我想要的任何东西。 示例控制器类定义和方法:

    public class ProductsController : ApiController
    {
        public IEnumerable<Keyword> GetKeywords(ODataQueryOptions<xxx.yyy.Keyword> options)
        {
          //my controller stuff
        }
    }    
    

    请注意,我将 ODataQueryOptions 转换为要应用选项的目标类型。

    【讨论】:

      猜你喜欢
      • 2012-03-11
      • 2019-10-15
      • 2017-12-04
      • 2013-08-24
      • 2014-06-11
      • 1970-01-01
      • 2015-02-06
      • 2011-10-08
      • 2015-09-04
      相关资源
      最近更新 更多