c# - 使用 OData 查询字符串时抛出 MissingManifestResourceException

标签 c# asp.net-core odata

我有一个使用 Microsoft.AspNetCore.OData.vNext 包的 ASPNet Core Web API 应用程序。当使用查询字符串尝试利用 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;
}

这是 URL 和查询字符串:http://localhost/api/locations ?$filter=Name%20eq%20'鲍勃'

'Name'是模型中指定的字符串字段,是数据库中的列。

堆栈跟踪:

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()

有人能指出我正确的方向吗?

最佳答案

我不知道您的解决方案中有什么,但是当我遇到这个问题时,我只是从我的 Controller 中删除 [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();
}

然后,在你的 Controller 中你应该有:

[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”现在应该可以正常工作了。

也许,您可以发布更详细的代码,以便我们能够为您提供帮助。希望你解决了你的问题。

关于c# - 使用 OData 查询字符串时抛出 MissingManifestResourceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43102465/

相关文章:

azure-devops - Azure DevOps Repo 有 OData 查询吗?

wcf - Odata 压缩 - 有任何支持吗? (适用于 WinRT 的 WCF 数据服务 5.0)

c# - 使用 SocketIO4Net 将函数回调从 .NET 应用程序传递到 node.js

c# - 如何设置 ASP :Menu with CSS 的样式

controller - Asp.net Core 中多个声明的基于声明的授权

c# - 如何通过反射匹配名称和参数类型来获取 protected 方法?

odata - 如何实现像官方示例服务这样的 OData V4 服务?

c# - 数据表,超越,加入

c# - 在 NUnit 3 中使用 [TestCase] 属性测试异常?

c# - 在 ASP.NET Core 6 中向 Program.cs 添加命名空间和 Main 方法是否会产生任何负面影响?