Api ASP.NET Web 应用程序 (WebForms) 项目显示 400

Api for ASP.NET Web Application (WebForms) Project showing 400

我是 .NET 框架的新手,我的公司还没有使用 Core,所以我想弄清楚为什么我的 Web 应用程序 api 显示 400。我有一个正常的web 表单项目并添加了一个名为 TagController.cs 的控制器 class。我的项目在端口 44318 上,我尝试访问 localhost/44318/api/tag 但没有成功。我还尝试添加一个控制器文件夹,其中包含 api 子文件夹和其中的控制器,但无济于事。我已经发布了我的项目层次结构和错误本身的图像。我有一种感觉,没有 global.asax 的项目可能与它有关,但另一个项目中有一个。也许 TagController.cs 指向另一个端口?非常感谢任何帮助。

TagController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ClarityDBWebFormsRedis;
using StackExchange.Redis;

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

        // GET api/<controller>/5
        public string Get(string data)        {
            
            return "doge";
        }

        // 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)
        {
        }
    }
}

您需要在项目中进行默认(路由)配置,以便它知道应该如何处理 ApiControllers,或者如何调用 API。这是在 Global.asax 中定义的示例。您可以简单地将 class TagController 放入名为“Controllers”的文件夹中。

Global.asax 相应地看起来例如像这样:

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 = RouteParameter.Optional });
}

例如,ApiController 如下所示:

public class PingController : ApiController
{
    [HttpGet, AllowAnonymous]
    public IHttpActionResult Get()
    {
        return Ok();
    }
}

对于普通页面,创建一个.Aspx页面,然后根据创建的文件夹结构在浏览器中调用即可。如果您使用 MVC,则此页面会在项目中的不同文件和文件夹中创建(Views/Home.cshtml、Models/HomeViewModel.cs 和 Controllers/HomeController.cs)。