0

我使用教程创建了一个本地托管的 Web API OData 服务。它利用了同样在本地托管并通过代码优先方法创建的数据库。它在系统中有两个实体,供应商和产品。当我使用 localhostXXXX/Products 或 /Suppliersit 从浏览器访问 api 时,会像这样显示提要:

{
  "@odata.context":"http://localhost:64469/$metadata#Products","value":[
    {
      "Id":1,"Name":"Broken Age","Price":15.00,"Category":"Adventure","SupplierId":1
    },{
      "Id":2,"Name":"Final Fantasy IX","Price":10.00,"Category":"JRPG","SupplierId":2
    },{
      "Id":3,"Name":"Fallout 3","Price":15.00,"Category":"Action RPG","SupplierId":3
    }
  ]

但是,当我尝试通过 PowerQuery 访问 Excel 中的提要时(通过转到来自其他来源 - > 从 OData 提要),它会显示一条提示消息:“禁止访问资源”。

谁能理解这是为什么?还有我怎么能绕过它?我没有对 Web 服务进行任何身份验证,并且由于它在 Web 浏览器中运行,因此可以清楚地访问它。

非常感谢任何人都可以提供的任何帮助。

来源见下文。

控制器

public class SuppliersController : ODataController
    {
        ProductsContext db = new ProductsContext();

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }


        [EnableQuery]
        public IQueryable<Supplier> Get()
        {
            return db.Suppliers;
        }
        [EnableQuery]
        public SingleResult<Supplier> Get([FromODataUri] int key)
        {
            IQueryable<Supplier> result = db.Suppliers.Where(p => p.Id == key);
            return SingleResult.Create(result);
        }

        [EnableQuery]
        public IQueryable<Product> GetProducts([FromODataUri] int key)
        {
            return db.Suppliers.Where(m => m.Id.Equals(key)).SelectMany(m => m.Products);
        }
    }


public class ProductsController : ODataController
{
    ProductsContext db = new ProductsContext();
    private bool ProductExists(int key)
    {
        return db.Products.Any(p => p.Id == key);
    }
    protected override void Dispose(bool disposing)
    {
        db.Dispose();
        base.Dispose(disposing);
    }

    [EnableQuery]
    public IQueryable<Product> Get()
    {
        return db.Products;
    }
    [EnableQuery]
    public SingleResult<Product> Get([FromODataUri] int key)
    {
        IQueryable<Product> result = db.Products.Where(p => p.Id == key);
        return SingleResult.Create(result);
    }

    [EnableQuery]
    public SingleResult<Supplier> GetSupplier([FromODataUri] int key)
    {
        var result = db.Products.Where(m => m.Id == key).Select(m => m.Supplier);
        return SingleResult.Create(result);
    }
}

楷模:

public class Supplier
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public ICollection<Product> Products { get; set; }
    }



public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
        public string Category { get; set; }

        [ForeignKey("Supplier")]
        public int? SupplierId { get; set; }
        public virtual Supplier Supplier { get; set; }
    }

网络配置

<connectionStrings>
    <add name="ProductsContext" connectionString="Data Source=(localdb)\v11.0; 
        Initial Catalog=ProductsContext; Integrated Security=True; MultipleActiveResultSets=True; 
        AttachDbFilename=|DataDirectory|ProductsContext.mdf"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

WebApiConfig

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // New code:
            ODataModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<Product>("Products");
            builder.EntitySet<Supplier>("Suppliers");
            config.MapODataServiceRoute(
                routeName: "ODataRoute",
                routePrefix: null,
                model: builder.GetEdmModel());
        }
    }

配置.cs

internal sealed class Configuration : DbMigrationsConfiguration<ProductService.Models.ProductsContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        ContextKey = "ProductService.Models.ProductsContext";
    }

    protected override void Seed(ProductService.Models.ProductsContext context)
    {
        context.Suppliers.AddOrUpdate(x => x.Id,
            new Supplier() {Id = 1, Name = "Double Fine" },
            new Supplier() { Id = 2, Name = "Square Enix" },
            new Supplier() { Id = 3, Name = "Bethesda" });

        context.Products.AddOrUpdate(x => x.Id,
            new Product() { Id = 1, SupplierId = 1, Name = "Broken Age", Price=15.00M, Category = "Adventure"},
            new Product() { Id = 2, SupplierId = 2, Name = "Final Fantasy IX", Price = 10.00M, Category = "JRPG" },
            new Product() { Id = 3, SupplierId = 3, Name = "Fallout 3", Price = 15.00M, Category = "Action RPG" });
    }
}

ProductServiceContext.cs

public class ProductsContext : DbContext
{
    public ProductsContext()
        : base("name=ProductsContext")
    {
    }
    public DbSet<Product> Products { get; set; }
    public DbSet<Supplier> Suppliers { get; set; }
}

更新

正如 Curt Hagenlocher 所问的,如果我在浏览器中访问 localhost64469/$metadata,以下是输出:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
<edmx:DataServices>
<Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="ProductService.Models">
<EntityType Name="Product">
<Key>
<PropertyRef Name="Id"/>
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false"/>
<Property Name="Name" Type="Edm.String"/>
<Property Name="Price" Type="Edm.Decimal" Nullable="false"/>
<Property Name="Category" Type="Edm.String"/>
<Property Name="SupplierId" Type="Edm.Int32"/>
<NavigationProperty Name="Supplier" Type="ProductService.Models.Supplier"/>
</EntityType>
<EntityType Name="Supplier">
<Key>
<PropertyRef Name="Id"/>
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false"/>
<Property Name="Name" Type="Edm.String"/>
<NavigationProperty Name="Products" Type="Collection(ProductService.Models.Product)"/>
</EntityType>
</Schema>
<Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Default">
<EntityContainer Name="Container">
<EntitySet Name="Products" EntityType="ProductService.Models.Product">
<NavigationPropertyBinding Path="Supplier" Target="Suppliers"/>
</EntitySet>
<EntitySet Name="Suppliers" EntityType="ProductService.Models.Supplier">
<NavigationPropertyBinding Path="Products" Target="Products"/>
</EntitySet>
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
4

2 回答 2

1

问题在于我的 Excel 的 PowerQuery 插件版本。这是一个过时的版本(我相信是 2.21 而不是 2.22),在最新版本中它添加了 OData v4 支持,这是我正在使用的 OData 版本。

谢谢你的帮助。

于 2015-06-03T16:00:01.553 回答
0

您可能想查看 PowerQuery 发送到您的服务的请求以及您的服务如何处理它。只有在服务返回时,PQ 才会报告禁止错误。

于 2015-05-28T00:39:05.433 回答