如何在 Asp.Net (aspx) 应用程序 C# 中添加 API 控制器 (Web API 2)
How to Add API controller (Web API 2) in Asp.Net (aspx) application C#
我在 .net framework 4.5 上有一个 asp.net (aspx) c# web 应用程序,我必须在此应用程序中创建一个将在第三方 CMS(Infusionsoft) Http 中使用的 Web api POST 活动。
在 asp.net C# 应用程序中添加 webapi 控制器
第 1 步:使用“添加新项”过程添加新的 webapi 控制器
第 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 方法中添加路由信息
添加使用命名空间:
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' 失败。
程序集 'DebtFREE, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 标有 AllowPartiallyTrustedCallersAttribute,并使用 2 级安全透明模型。 2 级透明度导致 AllowPartiallyTrustedCallers 程序集中的所有方法在默认情况下变得安全透明,这可能是导致此异常的原因。
第 5 步:转到 AssemblyInfo.cs (Bin/Properties/AssemblyInfo.cs) 并在行下方评论。
[程序集:AllowPartiallyTrustedCallers]
第 6 步:干杯,运行 申请并浏览 url:http://localhost:2071/api/payment
API 正在收集 aspx (asp.net) 应用程序。
我在 .net framework 4.5 上有一个 asp.net (aspx) c# web 应用程序,我必须在此应用程序中创建一个将在第三方 CMS(Infusionsoft) Http 中使用的 Web api POST 活动。
在 asp.net C# 应用程序中添加 webapi 控制器
第 1 步:使用“添加新项”过程添加新的 webapi 控制器
第 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 方法中添加路由信息
添加使用命名空间:
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' 失败。 程序集 'DebtFREE, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 标有 AllowPartiallyTrustedCallersAttribute,并使用 2 级安全透明模型。 2 级透明度导致 AllowPartiallyTrustedCallers 程序集中的所有方法在默认情况下变得安全透明,这可能是导致此异常的原因。
第 5 步:转到 AssemblyInfo.cs (Bin/Properties/AssemblyInfo.cs) 并在行下方评论。
第 6 步:干杯,运行 申请并浏览 url:http://localhost:2071/api/payment API 正在收集 aspx (asp.net) 应用程序。