【问题标题】:Odata Functions with Parameters throw 500带参数的 Odata 函数抛出 500
【发布时间】:2016-04-20 21:01:19
【问题描述】:

我的一个控制器有点问题。

每次我尝试使用参数调用某个函数时都会抛出 500,而在调试时我可以看到该函数甚至没有被调用。

首先是我的 WebApiConfig:

public static class WebApiConfig
{
        public static void Register(HttpConfiguration config)
        {
            ODataModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<Entity>("Entities");
            builder.EntitySet<DataTypes>("DataTypes");
            builder.EntitySet<ObjectValue>("ObjectValues");
            builder.EntitySet<Attributes>("Attributes");
            builder.EntitySet<Objects>("Objects");
            builder.Namespace = "EAVService.Controllers";
            builder.Action("FullAttributes").Returns<IHttpActionResult>()
                .CollectionParameter<Attributes>("Attributes");
            builder.Action("FullValues").Returns<IHttpActionResult>()
                .CollectionParameter<ObjectValue>("ObjectValue");

            config.MapODataServiceRoute(
                routeName: "ODataRoute",
                routePrefix: "odata",
                model: builder.GetEdmModel());
            config.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;
        }
    }

我的实体:

[Table("ObjectValue")]
public partial class ObjectValue
{
    public ObjectValue()
    {


    }
    [Key]
    [Column(Order = 0)]
    public int ObjectId { get; set; }

    [Key]
    [Column(Order = 1)]
    [StringLength(50)]
    public string Attribute { get; set; }

    [StringLength(256)]
    public string AttributeVal { get; set; }

    public virtual Attributes Attributes { get; set; }

    public virtual Objects Objects { get; set; }
}

和我的控制器:

public class ObjectValuesController : ODataController
{
    private EAVModel db;

    public ObjectValuesController(IDbConnectionProvider provider)
    {
        db = new EAVModel(provider.GetDbConnection());
    }

    // GET: odata/ObjectValues
    [EnableQuery]
    public IQueryable<ObjectValue> GetObjectValues()
    {
        IQueryable<ObjectValue> query = db.ObjectValue.AsQueryable();
        return query;
    }

    // GET: odata/ObjectValues(5)
    [EnableQuery]
    public IQueryable<ObjectValue> GetObjectValues([FromODataUri] string key)
    {
        IQueryable<ObjectValue> result = db.ObjectValue.Where(objectValue => objectValue.ObjectId == Convert.ToInt32(key)).AsQueryable();

        return result;
    }

.... }

第一个 Get 方法工作正常。

当涉及到带有参数的第二个 Get 时,我得到一个内部服务器错误。

http://localhost:80/EAVServiceAPI/odata/ObjectValues(1)

谁能给我提示一下可能出了什么问题?

问候 安德烈

【问题讨论】:

    标签: odata asp.net-web-api2


    【解决方案1】:

    带有键的方法应该返回ObjectValue 而不是IQueryable&lt;ObjectValue&gt; 并且key 参数错误,它与ObjectValue 对象上的键不匹配。你的意思是在ObjectIdAttribute 上拥有关键属性ObjectValue?如果是这样,您需要在 GetObjectValues 方法上有 2 个键参数,其名称与键匹配,否则删除 Key 属性之一并确保 key 参数的类型与 @987654332 上的键类型匹配@。

    【讨论】:

    • Ok.. 所以我将方法更改为: public ObjectValue GetObjectValues([FromODataUri] int ObjectId, string Attribute) {...} 当我尝试使用以下方法调用函数时:link 我现在得到了错误:{ "error":{ "code":"","message":"未找到与请求 URI 'localhost/EAVServiceAPI/odata/…"匹配的 HTTP 资源:{ "message":"未找到路由约定来选择一个使用模板 '~/entityset/key' 对 OData 路径执行操作。","type":"","stacktrace":"" } } } 问候安德烈
    • 你真的想要 2 把钥匙吗? ObjectId 听起来像是我的钥匙。
    • 是的,我想键入。 ObjectId 和 Attribute 是来自不同表的外键。问候安德烈
    • 好的,也许尝试将[FromODataUri] 也添加到Attribute 参数中?
    • 对..应该在那里..错过了:) public ObjectValue GetObjectValues([FromODataUri] int ObjectId, [FromODataUri] string Attribute) 但还是同样的错误
    【解决方案2】:

    我不知道我在配置中到底做错了什么,但这解决了我的问题:

    http://localhost/EAVServiceAPI/odata/ObjectValues?objectId=1

    问候 安德烈

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-22
      • 2013-10-28
      • 2021-06-24
      • 2018-08-03
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      • 2016-04-21
      相关资源
      最近更新 更多