Web API beside ASMX in the same project?

Web API beside ASMX in the same project?

Ask Question

Asked 7 years, 3 months ago

Modified 7 years, 3 months ago

Viewed 2k times

4

3

I have some web services in a asp.net web application using asmx.

as it goes I need to provide more web services here and I'm going to use Web API instead of traditional asmx.

the question is, Can I have these to types of web service in the same project which is going to deploy on the same web host?

here is the solution explorer:

as you can see, the CNIS.asmx and Webservise_TCIKH.asmx are serving from long time ago, and now I need to add more web services using Web API, but the old web services should remain functional .

I've added a new API controller called AirKindController.cs in the folder called CNISAPI. this is the implementation:

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

BUT, when I request the http://localhost:3031/CNISAPI/api/AirKind or http://localhost:3031/api/AirKind there is an error of 404! Am I calling right? or it is not possible to have these two types of web services in the same place?

Thanks in advance!

EDIT: (solution)

by @moarboilerplate guidance, I've added a Global.asax to my project and add the route configuration in the Application_Start method like below:

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

the WebApiConfig.Register is here:

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

Now I'm able to get the xml formatted output by requesting http://localhost:3031/api/AirKind.

Problem SOLVED!!!

asp.netweb-servicessoapasmxasp.net-web-api

Share

Follow

2,13633 gold badges2525 silver badges3535 bronze badges

  • 4

    You need to configure the route/s for your API controller/s when your application starts up. If you're doing that, can you post your config code? 

    – moarboilerplate

     May 28, 2015 at 13:56 
  • Also yes, you can do this. 

    – moarboilerplate

     May 28, 2015 at 13:56
  • I doesn't have any route in my solution, the project template was the old asp.net 3.5. I just add the ApiController class . how to do it? 

    – vaheeds

     May 28, 2015 at 14:05
  • in the global.asax, I add this: protected void Application_Start(object sender, EventArgs e) { HttpConfiguration config=new HttpConfiguration(); config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } it is not work so far! 

    – vaheeds

     May 28, 2015 at 14:18 
  • Thanks @moarboilerplate, it is Solved! my above code addition was incorrect! I've added the solution in the EDIT section of the question. 

    – vaheeds

     May 28, 2015 at 15:12 

Add a comment

1 Answer

Sorted by:

                                              Highest score (default)                                                                   Trending (recent votes count more)                                                                   Date modified (newest first)                                                                   Date created (oldest first)                              

1

As this problem is solved in the question itself, I decide to add the answer in the Answers section:

by @moarboilerplate guidance, I've added a Global.asax to my project and add the route configuration in the Application_Start method like below:

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

the WebApiConfig.Register is here:

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

Now I'm able to get the xml formatted output by requesting http://localhost:3031/api/AirKind.

Special thanks to @moarboilerplate .

Share

Follow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值