2

ASP.Net MVC 4 - Web API に問題があります。

API コントローラーに次のアクションがあります。

public void Post([FromBody] Integration integration)
{
    _repository.Add(integration);
}

何らかの理由で、私のブレークポイント_repository.Add(integration);が通過しません。

これが私が使用して投稿しているものですWebClient

POST https://ef52667f-3a6e-4a58-b548-e82b72186b5f.o365apps.net/api/integration HTTP/1.1
AccessToken: Vk1Ga4/L/BHYGNY1wVoq3tgGmVlu0YfPukZTlNqnFEK0HH
Content-Type: application/x-www-form-urlencoded
Host: ef52667f-3a6e-4a58-b548-e82b72186b5f.o365apps.net
Content-Length: 334
Expect: 100-continue

IntegrationId=c7456461-9e99-4e82-85d5-072670d270b5&Key=2999A604-30C9-423B-8500-8706673EEAFF&ApiUrl=https%3a%2f%2fapi.site.com%2fintegration.asmx&Web=https%3a%2f%2fsiteapp.sharepoint.com%2f&RemoteWeb=https%3a%2f%2ftesting.site.com%2fintegration%2f&ListName=Issues&IncomingAllowed=True&OutgoingAllowed=True&DeletionAllowed=True

これが私が得る応答です:

HTTP/1.1 500 Internal Server Error
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 36
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
Set-Cookie: ARRAffinity=5d9ceead697ee36c96ed0c04e0e51d80b3f1f3f7fced5c04c89cc370205e464d;Path=/;Domain=ef52667f-3a6e-4a58-b548-e82b72186b5f.o365apps.net
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-Powered-By: ARR/2.5
X-Powered-By: ASP.NET
Set-Cookie: WAWebSiteSID=3317c656d3214c81a19cbff73bb5d6cf; Path=/; HttpOnly
Date: Wed, 20 Mar 2013 20:02:44 GMT

{"Message":"An error has occurred."}

興味深いことに、私は完全にうまく機能する別の同様の方法を持っています:

public AuthResponse Post([FromBody] AuthRequest authRequest)
{
    var authResponse = new AuthResponse();

    try
    {
        . . .  
    }
    catch (Exception e)
    {
        authResponse.Success = false;
        authResponse.Message = e.Message;
    }

    return authResponse;
}

ここで何か不足していますか?

参考までに、ここで私のIntegrationモデル:

[SharePointList("Integrations")]
public class Integration : Entity
{
    public Integration(int id) : base(id) { }

    public Integration() { }

    [Required]
    public string ApiUrl { get; set; }

    [Required]
    public bool DeletionAllowed { get; set; }

    [Required]
    public bool IncomingAllowed { get; set; }

    [IgnoreDataMember]
    [Required]
    public Guid IntegrationId { get; set; }

    [IgnoreDataMember]
    [Required]
    public string Key { get; set; }

    [Required]
    public string ListName { get; set; }

    [Required]
    public bool OutgoingAllowed { get; set; }

    [Required]
    public string RemoteWeb { get; set; }

    [Required]
    public string Web { get; set; }

    public override object this[string property]
    {
        get { return GetType().GetProperty(property).GetValue(this, null); }
        set
        {
            if (property.Equals("Id")) throw new Exception("Cannot set Id through an indexer.");

            if (property.Equals("IntegrationId"))
            {
                IntegrationId = new Guid((string) value);
                return;
            }

            if (property.Equals("Title"))
            {
                Key = value as string;
                return;
            }

            PropertyInfo propertyInfo = GetType().GetProperty(property);
            if (propertyInfo == null) throw new Exception(property + " is not a valid property.");

            propertyInfo.SetValue(this, value, null);
        }
    }
}

Entity.cs

public abstract class Entity : IEntity
{
    protected Entity(int id)
    {
        Id = id;
    }

    protected Entity() { }

    [IgnoreDataMember]
    public int Id { get; private set; }

    public abstract object this[string property] { get; set; }
}

IEntity.cs

public interface IEntity
{
    int Id { get; }

    object this[string property] { get; set; }
}
4

1 に答える 1

0

クラス/プロパティの上に DataContract/DataMember 属性を配置することを含む解決策は、問題の適切な解決策ではありません。

WCF の残骸を取り除くために必要なことは次のとおりです。

これを global.asax に追加します。

        GlobalConfiguration.Configuration.Services.RemoveAll(typeof(ModelValidatorProvider), v => v is InvalidModelValidatorProvider);
        GlobalConfiguration.Configuration.Formatters.JsonFormatter.RequiredMemberSelect‌or = new SuppressRequiredMemberSelector();

SupressRequiredMemberSelector クラスを作成します。

public class SuppressRequiredMemberSelector : IRequiredMemberSelector
{
    public bool IsRequiredMember(MemberInfo member)
    {
        return false;
    }
}

これにより、注釈を変更する必要なく問題が解決され、Web API が提供する拡張性を使用するため、Web API の「バグ修正」として広く受け入れられています。

于 2013-03-20T22:18:05.997 に答える