Azure - Web API + OData - 过滤查询验证器 - 角色保持回收

时间:2013-09-19 04:25:53

标签: asp.net azure odata asp.net-web-api asp.net-web-api-odata

我刚刚部署了包含WebAPI的WCF Web服务。一切正常,直到我将自己的Filter Query Validator添加到我的项目中。然后角色继续回收和重新启动。这是一个非常奇怪的问题,我无法确定这是什么原因。谢谢你的帮助。

using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.OData.Query;
using System.Web.Http.OData.Query.Validators;
using EMP.WebServices.api.Providers;
using Microsoft.Data.Edm.Library;
using Microsoft.Data.OData.Query.SemanticAst;

namespace EMP.WebServices.api.Validators
{
    public class EntityFilterByQueryValidator : FilterQueryValidator
    {
        public override void ValidateSingleValuePropertyAccessNode(SingleValuePropertyAccessNode propertyAccessNode, ODataValidationSettings settings)
        {
            var declaringType = propertyAccessNode.Property.DeclaringType;

            var edmEntityType = declaringType as EdmEntityType;

            var allowedProperties = TableStorageProvider.GetMetadataPropertyNames(edmEntityType.Name,
                                                                                  Constants.WebApi.V1).ToList();

            // Validate if we are accessing some sensitive property of WorkItem, such as Votes
            if (!allowedProperties.Contains(propertyAccessNode.Property.Name))
            {
                throw new HttpResponseException(new HttpResponseMessage
                {
                    Content = new StringContent(string.Format("{0} is an invalid filter property. {1}You can filter by these properties:{1}{2}.", propertyAccessNode.Property.Name, Environment.NewLine, string.Join(", " + Environment.NewLine, allowedProperties))),
                    ReasonPhrase = "Invalid Filter Property",
                    StatusCode = HttpStatusCode.BadRequest

                });
            }

            base.ValidateSingleValuePropertyAccessNode(propertyAccessNode, settings);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我从ODataQueryableSample(http://www.asp.net/web-api/samples)看到,抛出ODataException似乎是首选技术。我建议你尝试一下。

如果您认为此处存在错误,建议您在http://aspnetwebstack.codeplex.com/workitem/list/basic报告新错误。