2017-12-31 50 views
0

방금 ​​크로스 플랫폼 Xamarin 솔루션 내에 웹 API 프로젝트를 만들었습니다. 지금, 나는 다음과 같은 http://www.c-sharpcorner.com/article/asp-net-web-api-and-sql-server/asp-net-web-api-and-sql-server

내가 방금 만든 기사 다음 SQL Server 데이터베이스에 웹 API를 연결하려면 : 모든

먼저 내 솔루션 내에서 웹 API 프로젝트를 추가했다.

이상이 웹 API를 내 로컬 컴퓨터에 설치된 SQL Server에 연결하기 위해 Ado.Net 항목이있는 클래스 라이브러리를 추가했습니다. 나는 다음과 같은 SQL 서버 안에 내 데이터를 참조 할 수이 Ado.Net에

: 웹 API에

namespace SqlServerConect { 
using System; 
using System.Data.Entity; 
using System.Data.Entity.Infrastructure; 

public partial class Icquire_databaseEntities : DbContext 
{ 
    public Icquire_databaseEntities() 
     : base("name=Icquire_databaseEntities") 
    { 
    } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     throw new UnintentionalCodeFirstException(); 
    } 

    public virtual DbSet<Business_Register> Business_Register { get; set; } 
    public virtual DbSet<Product_Register> Product_Register { get; set; } 
} 

}

namespace SqlServerConect { 
using System; 
using System.Collections.Generic; 

public partial class Business_Register 
{ 
    public int Id { get; set; } 
    public string BusinessName { get; set; } 
    public string BusinessFantasyName { get; set; } 
    public string BusinessArea { get; set; } 
    public string BusinessSpecificArea { get; set; } 
    public string Country { get; set; } 
    public string State { get; set; } 
    public string City { get; set; } 
    public string Address { get; set; } 
    public string Neighbourhood { get; set; } 
    public string Number { get; set; } 
    public string Apartment { get; set; } 
    public decimal Latitude { get; set; } 
    public decimal Longitude { get; set; } 
    public System.DateTime DateofReg { get; set; } 
    public Nullable<System.DateTime> DateofMod { get; set; } 
    public string BusinessStatus { get; set; } 
    public string RegisteredBy { get; set; } 
} 

}

프로젝트 ADO.NET 프로젝트에 대한 참조를 추가하고 새 컨트롤러를 만들었습니다.

using SqlServerConect; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Web.Http; 

namespace WEBAPI.Controllers { 
public class IcquireDBController : ApiController 
{ 
    public IEnumerable<Business_Register> Get() 
    { 
     using (Icquire_databaseEntities entities = new Icquire_databaseEntities()) 
      return entities.Business_Register.ToList(); 
    } 
} 

}의 Web.config 폴더에

나는 다음과 같이의 app.config에 생성 된 코드를 추가 :

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
Para obter mais informações sobre como configurar seu aplicativo ASP.NET, visite 
https://go.microsoft.com/fwlink/?LinkId=301879 --> 
<configuration> 
<connectionStrings> 
<add name="Icquire_databaseEntities" connectionString="metadata=res://*/SqlServerConnect.csdl|res://*/SqlServerConnect.ssdl|res://*/SqlServerConnect.msl;provider=System.Data.SqlClient;provider connection string='data source=ICQUIRE;initial catalog=&quot;Icquire database&quot;;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework'" providerName="System.Data.EntityClient" /> 
</connectionStrings> 
<appSettings> 
<add key="webpages:Version" value="3.0.0.0" /> 
<add key="webpages:Enabled" value="false" /> 
<add key="ClientValidationEnabled" value="true" /> 
<add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
</appSettings> 
<system.web> 
<compilation debug="true" targetFramework="4.6.1" /> 
<httpRuntime targetFramework="4.6.1" /> 
<httpModules> 
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" /> 
</httpModules> 
</system.web> 
<system.webServer> 
<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" /> 
<modules> 
    <remove name="TelemetryCorrelationHttpModule" /> 
    <add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="integratedMode,managedHandler" /> 
    <remove name="ApplicationInsightsWebTracking" /> 
    <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" /> 
</modules> 
</system.webServer> 
<runtime> 
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
    <dependentAssembly> 
    <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /> 
    <bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.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.6.5135.21930" newVersion="1.6.5135.21930" /> 
    </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.WebPages" 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="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" /> 
    <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" /> 
    </dependentAssembly> 
</assemblyBinding> 
</runtime> 
<system.codedom> 
<compilers> 
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /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.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" /> 
</compilers> 
</system.codedom> 
</configuration>  

음, 기사,이 작동한다, 그러나 웹 API 프로젝트를 디버그하고 상태를 확인하려고하면 Explorer는 페이지 http://localhost:63952/을 올바르게로드하지만 페이지를로드 할 때 은 다음 화면을 표시합니다.

<Error> 
<Message> 
Não foram encontrados recursos HTTP que correspondam ao URI de solicitação 'http://localhost:63952/api/Business_register'. 
</Message> 
<MessageDetail> 
Nenhum tipo foi encontrado que corresponda ao controlador chamado 'Business_register'. 
01 23,516,

영어로 Traducing :

<Error> 
<Message> 
No HTTP resources matching the Request URI 'http: // localhost: 63952/api/Business_register' were found. 
</ Message> 
<MessageDetail> 
No type was found with corresponding to the driver named 'Business_register'. 
</ MessageDetail> 
</Error> 

는 사람이 나에게 빛을 줄 수 있습니까?

미리 감사드립니다.

다니엘.

답변