C# WebAPI不发送数据

C# WebAPI不发送数据,c#,sql-server,asp.net-web-api,ado.net,C#,Sql Server,Asp.net Web Api,Ado.net,我正在使用Visual Studio 2015开发最简单的WebAPI,试图公开数据库 我使用DB(SQL SERVER)连接字符串生成ADO.NET实体,因此我可以连接DB 最简单的WebAPI函数: // GET: api/avl_users public IQueryable<avl_user> Getavl_users() { return db.avl_users; } //GET:api/avl\u用户 公共图书馆 My Web.config: <?xml

我正在使用Visual Studio 2015开发最简单的WebAPI,试图公开数据库

我使用DB(SQL SERVER)连接字符串生成ADO.NET实体,因此我可以连接DB

最简单的WebAPI函数:

// GET: api/avl_users
public IQueryable<avl_user> Getavl_users()
{
    return db.avl_users;
}
//GET:api/avl\u用户
公共图书馆

My Web.config:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301879
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-deletemeWebAPI-20161116123948.mdf;Initial Catalog=aspnet-deletemeWebAPI-20161116123948;Integrated Security=True" providerName="System.Data.SqlClient" />
    <add name="avlEntities" connectionString="metadata=res://*/Models.AvlModel.csdl|res://*/Models.AvlModel.ssdl|res://*/Models.AvlModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQLEXPRESS;initial catalog=avl;persist security info=True;user id=xxxxx;password=yyyyyy;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="deletemeWebAPIContext" connectionString="Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=deletemeWebAPIContext-20161116125552; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|deletemeWebAPIContext-20161116125552.mdf" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings></appSettings>
  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5.1" />
    <httpRuntime targetFramework="4.5.1" />
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
    </httpModules>
  </system.web>
  <system.webServer>
    <modules>
      <remove name="FormsAuthentication" />
      <remove name="ApplicationInsightsWebTracking" />
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
    </modules>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
    <validation validateIntegratedModeConfiguration="false" />
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
</configuration>

谁能给我点建议,让我继续找

谢谢
Carlos

您返回的是
IQueryable
对象,除非您在收到它之后以某种方式查询它,否则它不会提供数据。首先尝试查询它,例如:

public List<avl_user> Getavl_users()
{
    return db.avl_users.ToList();
}
public List Getavl_用户()
{
返回db.avl_users.ToList();
}

这是该死的日期时间字段!!!我测试了另一个没有DateTime值的实体集,它的性能更好,现在我只需要找出如何处理此错误:

{“Message”:“出现错误”。,“ExceptionMessage”:“ObjectContent
1”类型未能序列化内容类型“text/html;charset=utf-8”的响应正文。”,“ExceptionType”:“System.InvalidOperationException”,“StackTrace”:null,“InnerException”:{“Message”:“出现错误”。,“ExceptionMessage”:”:“自创建数据库以来,支持‘DeleTeMeWebApPixTimes’上下文的模型发生了变化。请考虑使用代码第一迁移来更新数据库(http://go.microsoft.com/fwlink/?LinkId=238269).,“ExceptionType”:“System.InvalidOperationException”,“StackTrace”:“位于System.Data.Entity.CreateDatabaseIfNotExists
1.InitializeDatabase(TContext context)\r\n在System.Data.Entity.Internal.InternalContext.c\uu DisplayClassf
1.b\u e()\r\n在System.Data.Entity.Internal.InternalContext.PerformilizationAction(Action Action Action)\r\n在System.Data.Entity.InternalContext.PerformDatabaseInitialization()\r\n在System.Data.Entity.Internal.LazyInternalContext.b\u 4(InternalContext c)\r\n位于System.Data.Entity.Internal.RetryAction
1.PerformAction(TInput输入)\r\n位于System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action
1 Action)\r\n位于System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()\r\n在System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)\r\n在System.Data.Entity.Internal.Linq.InternalSet
1.Initialize()\r\n在System.Data.Entity.Infrastructure.DbQuery
1.System.Collections.IEnumerable.GetEnumerator()\r\n位于Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter编写器,IEnumerable值,JsonArrayContract合同,JsonProperty成员,JsonContainerContract集合合同,JsonProperty容器属性)\r\n位于Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter编写器,对象值,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerProperty)\r\n位于Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter JsonWriter,对象值,类型objectType)\r\n位于Newtonsoft.Json.JsonSerializer.serializer\n(JsonWriter JsonWriter,对象值,类型objectType)\r\n位于System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(类型,对象值,流writeStream,编码有效编码)\r\n位于System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(类型,对象值,流writeStream writeStream,编码有效编码)\r\n位于System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(类型、对象值、流writeStream、HttpContent内容)\r\n位于System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(类型类型、对象值、流writeStream、HttpContent内容、TransportContext TransportContext、CancellationToken CancellationToken)\r\n--从引发异常的上一个位置开始的堆栈结束跟踪---\r\n在System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务任务)\r\n位于System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n位于System.Web.Http.WebHost.HttpControllerHandler.d_u1b.MoveNext()“}


我的key是一个名为'index'的字段,我必须在ADO.NET生成的类上添加[key]注释,我想知道我的[key]标签是否生成了验证错误。“自从数据库创建以来,上下文已经更改了”。

api返回什么?WebAPI说它连接到:public bool TestConnection(){using(var db=new deletemeWebAPIContext()){DbConnection conn=db.Database.Connection;尝试{conn.Open();返回true;}catch{return false;}}}它返回“[]”