Web API beside ASMX in the same project?
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
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? May 28, 2015 at 13:56 - Also yes, you can do this. 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
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 .
Follow