the first time you use "ASP.NET MVC4 WebAPI" to develop a project, you only need to use WebAPI because the front end invokes the rendering directly by HTML+Ajax.
< H2 > doubt 1 < / H2 >what elegant posture do the great gods use to avoid the growth of routing (including RouteAttribute registered routes)?
follow the WebAPI routing and ApiController examples given by Visual Studio
how to obtain the services activated by customers as follows?
1. Get the specified service
request method & URL address: [GET] http://*/api/service/ service number
2. Get all the services of the specified customer
request method & URL address: [GET] http://*/api/service/ customer number
Is this the only way to write?
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "serviceID",
routeTemplate: "api/{controller}/{serviceID}",
defaults: new { serviceID = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "untID",
routeTemplate: "api/{controller}/{untID}",
defaults: new { untID = RouteParameter.Optional }
);
}
}
public class ServiceController : ApiController
{
//1
public ServiceEntity Get(string serviceID) {...}
//2
public IEnumerable<ServiceEntity> Get(string untID) {...}
}
if you follow this line of thinking, don"t you have to register a lot of routes?
what about adding a new API?
3. Get the specified service and personalized configuration value
request method & URL address: [GET] http://*/api/service/full/ service number
is this the only way to write?
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "serviceID",
routeTemplate: "api/{controller}/{serviceID}",
defaults: new { serviceID = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "untID",
routeTemplate: "api/{controller}/{untID}",
defaults: new { untID = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "action_serviceID",
routeTemplate: "api/{controller}/{action}/{serviceID}",
defaults: new { serviceID = RouteParameter.Optional }
);
}
}
public class ServiceController : ApiController
{
//1
public ServiceEntity Get(string serviceID) {...}
//2
public IEnumerable<ServiceEntity> Get(string untID) {...}
//3
[HttpGet]
public ServiceAndConfigModel Full(string serviceID) {...}
}
< H2 > doubt 2 < / H2 >
according to the online RESTful introduction, the URL address of my third API should be (/ api/service/ service number / config or / api/service/ service number / config/ configuration number)
how to write the signature for such a URL method?
is it written in this way?
public class ServiceController : ApiController
{
//3
[HttpGet]
[Route("service/{serviceID}/config")]
public ServiceAndConfigModel Get(string serviceID) {...}
[HttpGet]
[Route("service/{serviceID}/config/{configID}")]
public ServiceAndConfigModel Get(string serviceID, string configID) {...}
}
my knowledge of .net WebAPI is only scratching the surface, so I"ll write down the solutions I"m using so far and ask the gods for advice.
< H2 > my current solution < / H2 >the solution I am currently using is to put all the parameters of the GET request to "?" Later, the URL parameter name must be the same as the Action parameter name. Implement your own ControllerSelector that supports namespaces.
the route registration code is as follows:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Services.Replace(typeof(IHttpControllerSelector), new NamespaceHttpControllerSelector(config)); //NamespaceHttpControllerSelector
.... Filter
config.Routes.MapHttpRoute(
name: "DefaultApiRoute",
routeTemplate: "api/{controller}/{action}"
);
config.Routes.MapHttpRoute(
name: "NamespaceApiRoute",
routeTemplate: "api/{namespace}/{controller}/{action}"
);
config.Routes.MapHttpRoute(
name: "Namespace2ApiRoute",
routeTemplate: "api/{namespace}/{namespace2}/{controller}/{action}"
);
}
}
The URL address of the request is as follows:
I don"t know if it"s RESTful style.
1. Get the specified service
/ api/unit/service/single?serviceID= service number
, where unit is the namespace name.
2. Get all services of the specified customer
/ api/unit/service/list?unitid= customer number
where unit is the namespace name.
3, get the specified service and personalization configuration value
/ api/unit/service/complete/single?serviceID= service number
, where unit and service are namespace names.
the Controllers code is as follows:
namespace xxx.Controllers.Unit
{
public class ServiceController : ApiController
{
//1
[HttpGet]
public ServiceEntity Single(string serviceID) {...}
//2
[HttpGet]
public IEnumerable<ServiceEntity> List(string untID) {...}
}
}
namespace xxx.Controllers.Unit.Service
{
public class CompleteController : ApiController
{
//3
[HttpGet]
public ServiceAndConfigModel Single(string serviceID) {...}
}
}