Search code examples
asp.netweb-servicessoapasmxasp.net-web-api

Web API beside ASMX in the same project?


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:

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!!!


Solution

  • 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 .