从Excel中的Power Query访问OData Feed:禁止访问资源

时间:2015-05-22 10:52:14

标签: sql-server excel asp.net-web-api odata powerquery

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

{
  "@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中的Feed时(通过转到其他来源 - >来自OData Feed),它会显示一条消息提示:"禁止访问资源"。

任何人都可以理解为什么会这样吗?我也可以如何绕过它?我没有对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; }
    }

的Web.Config

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

Configuration.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>

2 个答案:

答案 0 :(得分:0)

您可能希望查看PowerQuery发送给您的服务的请求以及您的服务对其执行的操作。如果服务返回,则仅在PQ中报告禁止错误。

答案 1 :(得分:0)

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

感谢您的帮助。