2017-03-29 50 views
4

我有一個正在使用Microsoft.AspNetCore.OData.vNext包的ASPNet Core Web API應用程序。當使用查詢字符串嘗試利用OData功能時,我收到MissingManifestResourceException。這裏是有問題的代碼:使用OData查詢字符串時拋出MissingManifestResourceException

[EnableQuery] 
[HttpGet()] 
public class LocationsController : Controller 
{ 
    public IActionResult GetLocations() 
    { 
    IQueryable<Location> locationEntities = _locationInfoRepo.GetLocations(); 
    if (locationEntities == null) 
    { 
     return NotFound(); 
    } 

    var results = Mapper.Map<IEnumerable<LocationDTO>>(locationEntities); 

    return Ok(results); 
    } 
} 

注:我已經試圖改變從IActionResult方法類型的IQueryable <>無濟於事。我也評論過Automapper Map方法,以確保在那裏沒有問題。

下面是正在使用EF返回數據的方法:

public IQueryable<Location> GetLocations() 
{ 
    return _context.Locations; 
} 

這裏的查詢字符串沿着網址:http://localhost/api/locations $過濾器=名稱%20當量%20'Bob '

' 名稱? '是模型中指定的字符串字段,並且是數據庫中的列。

堆棧跟蹤:

System.Resources.MissingManifestResourceException: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Microsoft.AspNetCore.OData.SRResources.resources" was correctly embedded or linked into assembly "Microsoft.AspNetCore.OData.vNext" at compile time, or that all the satellite assemblies required are loadable and fully signed. 
    at System.Resources.ManifestBasedResourceGroveler.HandleResourceStreamMissing(String fileName) 
    at System.Resources.ManifestBasedResourceGroveler.GrovelForResourceSet(CultureInfo culture, Dictionary`2 localResourceSets, Boolean tryParents, Boolean createIfNotExists, StackCrawlMark& stackMark) 
    at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo requestedCulture, Boolean createIfNotExists, Boolean tryParents, StackCrawlMark& stackMark) 
    at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents) 
    at System.Resources.ResourceManager.GetString(String name, CultureInfo culture) 
    at Microsoft.AspNetCore.OData.SRResources.GetString(String name, String[] formatterNames) 
    at Microsoft.AspNetCore.OData.SRResources.get_ClrTypeNotInModel() 
    at Microsoft.AspNetCore.OData.ODataQueryContext..ctor(IEdmModel model, Type elementClrType, ODataPath path) 
    at Microsoft.AspNetCore.OData.EnableQueryAttribute.OnActionExecuted(ActionExecutedContext context) 
    at Microsoft.AspNetCore.Mvc.Filters.ActionFilterAttribute.<OnActionExecutionAsync>d__6.MoveNext() 
    --- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextActionFilterAsync>d__25.MoveNext() 
    --- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
    at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context) 
    at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) 
    at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextExceptionFilterAsync>d__24.MoveNext() 

有人能指出我在正確的方向?

+0

要交叉引用我在WebApi GitHub項目中與OData團隊開放的問題,請參閱此問題的鏈接:https://github.com/Data/WebApi/issues/955 –

回答

0

我不知道你有什麼解決方案,但是當我遇到這個問題時,我已經解決了從我的控制器中刪除[RouteAttribute]的問題。讓我給你展示一個可行的例子。

在Startup.cs你應該有:

public void ConfigureServices(IServiceCollection services) 
{ 
    // Your stuff... 
    services.AddOData(opt => opt.RoutingConventions 
            .Insert(0, new DefaultODataRoutingConvention())); 
    // Your stuff... 
} 

public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
{ 
    // Your stuff... 
    var provider = app.ApplicationServices.GetRequiredService<IAssemblyProvider>(); 
    var model = GetEdmModel(provider); 
    app.UseMvc(builder => builder 
     .MapODataRoute("odata", model) 
     .MapRoute("default", "api/{controller}/{id?}")); 
} 

private static IEdmModel GetEdmModel(IAssemblyProvider assemblyProvider) 
{ 
    var builder = new ODataConventionModelBuilder(assemblyProvider); 
    builder.EntitySet<Product>("Products"); 

    return builder.GetEdmModel(); 
} 

然後,在你的控制器,你應該有:

[EnableQuery] 
public class ProductsController : Controller 
{   
    private readonly IProductDbContext _dbContext; 

    public ProductsController(IProductDbContext dbContext) 
    { 
     _dbContext = dbContext; 
    }   

    [HttpGet] 
    public IQueryable<Product> Get() 
    { 
     return _dbContext.Products.AsQueryable(); 
    } 
} 

隨着MapODataRoute中的OData可以找到ProductController的內部消除需要的任何屬性或自定義路由,你可以看看here

因此,不需要RouteAttribute,並且此查詢「http://localhost:44302/odata/Products?$ top = 2 & $ skip = 2」現在應該可以正常工作。

也許,您可以發佈更詳細的代碼,以便我們能夠幫助您。希望你解決了你的問題。

+0

這很有趣解。自從我遇到這個問題後,我決定轉移到完整的.NET架構,並轉向完整的OData。它似乎運行良好,但我有點沮喪,我無法讓AspNetCore正常工作。也許在我的下一個項目中,我會再次嘗試使用您的解決方案。感謝您的信息! –