2013-03-20 51 views
2

我在使用ASP.Net MVC 4 - Web API时遇到了一些问题。在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; } 
} 
+2

您是否可以在错误中包含异常详细信息并与堆栈跟踪共享错误响应。您可以使用此[属性](http://msdn.microsoft.com/en-us/library/system.web.http.httpconfiguration.includeerrordetailpolicy(v = vs.108).aspx)更改设置。 – 2013-03-20 20:33:54

+1

我很确定你有同样的问题在这里提到:http://stackoverflow.com/questions/14499244/problems-with-model-binding-and-validation-attributes-with-asp-net-web -api?lq = 1 – nemesv 2013-03-20 20:49:56

+0

这很有帮助。非常感谢你们。 – Moon 2013-03-20 21:41:05

回答

0

涉及将d解决方案您的类/属性顶部的ataContract/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的“错误修复”,因为它使用它提供的可扩展性,没有别的。

+0

您只是在更改JsonFormatter,您的解决方案是否可以使用XML?你提到这是“被广泛接受”的错误修复“”你能在你的文章中添加一些参考吗? – nemesv 2013-03-21 08:44:07

+0

您可以搜索周围,但这是报告问题的主要网站afaik:http://aspnetwebstack.codeplex.com/workitem/270 – 2013-03-21 10:11:09

相关问题