【问题标题】:Web API beside ASMX in the same project?同一个项目中ASMX旁边的Web API?
【发布时间】:2015-08-11 01:53:48
【问题描述】:

我在一个使用 asmx 的 asp.net Web 应用程序中有一些 Web 服务。

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

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

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

如您所见,CNIS.asmxWebservise_TCIKH.asmx 是很久以前的服务了,现在我需要使用 Web API 添加更多的 Web 服务,但旧的 Web 服务应该仍然可以正常工作。

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

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

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

提前致谢!

编辑:(解决方案)

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

protected void Application_Start(object sender, EventArgs e)
        {            
            GlobalConfiguration.Configure(WebApiConfig.Register);
        }

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 }
            );
        }
    }

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

问题解决了!!!

【问题讨论】:

  • 您需要在应用程序启动时为您的 API 控制器配置路由。如果你这样做,你能发布你的配置代码吗?
  • 也是的,你可以这样做。
  • 我的解决方案中没有任何路由,项目模板是旧的 asp.net 3.5。我只是添加了 ApiController 类。怎么办?
  • 在 global.asax 中,我添加了这个:protected void Application_Start(object sender, EventArgs e) { HttpConfiguration config=new HttpConfiguration(); config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute(名称:“DefaultApi”,routeTemplate:“api/{controller}/{id}”,默认值:new { id = RouteParameter.Optional }); } 到目前为止还不行!
  • 感谢@moarboilerplate,已解决!我上面的代码添加不正确!我已在问题的 EDIT 部分添加了解决方案。

标签: asp.net web-services soap asmx asp.net-web-api


【解决方案1】:

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

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

protected void Application_Start(object sender, EventArgs e)
        {            
            GlobalConfiguration.Configure(WebApiConfig.Register);
        }

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 }
            );
        }
    }

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

特别感谢@moarboilerplate。

【讨论】:

    猜你喜欢
    • 2018-06-17
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 2016-07-06
    • 1970-01-01
    • 2019-09-18
    • 2015-11-21
    • 2014-08-16
    相关资源
    最近更新 更多