同一项目中除了 ASMX 之外的 Web API?

vah*_*eds 5 asp.net soap web-services asmx asp.net-web-api

我在 ASP.NET Web 应用程序中使用了一些 Web 服务asmx

随着情况的发展,我需要在这里提供更多的 Web 服务,并且我将使用 Web API 而不是传统的 asmx。

问题是,我可以在同一项目中将这些类型的 Web 服务部署在同一 Web 主机上吗?

这是解决方案资源管理器:

解决方案浏览器

如您所见,CNIS.asmxWebservise_TCIKH.asmx很久以前就开始提供服务了,现在我需要使用 Web API 添加更多 Web 服务,但旧的 Web 服务应该保持功能。

AirKindController.cs在名为 CNISAPI 的文件夹中添加了一个新的 API 控制器。这是实现:

public class AirKindController : ApiController
{
    CNISDataContext db;
    private AirKindController()
    {
        db = new CNISDataContext();
    }
    // GET api/<controller>
    public IEnumerable<AirKind> Get()
    {
        return db.AirKinds;
    }
Run Code Online (Sandbox Code Playgroud)

但是,当我请求时,http://localhost:3031/CNISAPI/api/AirKind 还是http://localhost:3031/api/AirKind出现了 404 错误!我打电话对吗?或者不可能将这两种类型的网络服务放在同一个地方?

提前致谢!

编辑:(解决方案)

通过@moarboilerplate的指导,我Global.asax在我的项目中添加了一个,并在方法中添加了路由配置,Application_Start如下所示:

protected void Application_Start(object sender, EventArgs e)
        {            
            GlobalConfiguration.Configure(WebApiConfig.Register);
        }
Run Code Online (Sandbox Code Playgroud)

WebApiConfig.Register这里:

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
Run Code Online (Sandbox Code Playgroud)

现在我可以通过请求获取 xml 格式的输出http://localhost:3031/api/AirKind

问题解决了!!!

vah*_*eds 1

由于这个问题已在问题本身中得到解决,我决定在答案部分添加答案:

Application_Start通过@moarboilerplate的指导,我在我的项目中添加了一个Global.asax,并在如下方法中添加了路由配置:

protected void Application_Start(object sender, EventArgs e)
        {            
            GlobalConfiguration.Configure(WebApiConfig.Register);
        }
Run Code Online (Sandbox Code Playgroud)

WebApiConfig.Register这里:

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
Run Code Online (Sandbox Code Playgroud)

现在我可以通过请求获取 xml 格式的输出http://localhost:3031/api/AirKind

特别感谢@moarboilerplate。