2016-12-13 75 views
-3

我在.net framework 4.5上有一個asp.net(aspx)c#web應用程序,我必須在此應用程序中創建一個web api這將消耗在第三方CMS(Infusionsoft)Http POST活動中。如何在Asp.Net(aspx)應用程序中添加API控制器(Web API 2)C#

+3

祝你好運。現在你的問題到底是什麼? – Stormhashe

+0

說明!=問題。請閱讀:[如何問](http://stackoverflow.com/help/how-to-ask) – RandomStranger

+1

Dev-Systematix提出了一個面向目標的問題,但這是一個面向問題的網站。 – Amy

回答

0

要在asp.net C#應用程序中添加的WebAPI控制器

步驟1:使用添加新項過程添加新的WebAPI控制器 enter image description here

步驟2:添加PaymentController

public class PaymentController : ApiController 
    { 
     // GET api/<controller> 
     public IEnumerable<string> Get() 
     { 
      return new string[] { "value1", "value2" }; 
     } 

     // GET api/<controller>/5 
     public string Get(int id) 
     { 
      return "value"; 
     } 

     // POST api/<controller> 
     public void Post([FromBody]string value) 
     { 
     } 

     // PUT api/<controller>/5 
     public void Put(int id, [FromBody]string value) 
     { 
     } 

     // DELETE api/<controller>/5 
     public void Delete(int id) 
     { 
     } 
    } 

步驟3 :在Global.asax.cs文件中的Application_stat methd中添加路由信息

Add Using名稱空間:

using System.Web.Http; 
using System.Web.Routing; 



protected void Application_Start(Object sender, EventArgs e) 
     { 
      RouteTable.Routes.MapHttpRoute(
       name: "DefaultApi", 
       routeTemplate: "api/{controller}/{id}", 
       defaults: new { id = System.Web.Http.RouteParameter.Optional } 
       ); 
     } 

4步:運行應用程序,它會引發異常下面:

嘗試通過安全透明方法 'DebtFREE.Global.Application_Start(System.Object的,System.EventArgs)' 來訪問安全關鍵字段'System.Web.Http.RouteParameter.Optional'失敗。 Assembly'DebtFREE,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'用AllowPartiallyTrustedCallersAttribute標記,並使用2級安全透明度模型。級別2透明會導致AllowPartiallyTrustedCallers程序集中的所有方法默認情況下變爲安全透明,這可能是此異常的原因。

enter image description here

步驟5:進入的AssemblyInfo.cs(濱/屬性/ AssemblyInfo.cs中)和下面行註釋。 enter image description here [組件:AllowPartiallyTrustedCallers]

步驟6:乾杯,運行應用程序和瀏覽網址:http://localhost:2071/api/payment API被工作呢togather ASPX(asp.net)應用程序。

enter image description here

相關問題