账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    RESTful的.net webapi路由与URL的疑惑
    93
    0

    第一次使用“ASP.NET MVC4 WebAPI”开发项目,只需使用WebAPI因为前端直接HTML+Ajax调用呈现。

    疑惑1

    各位大神是用什么优雅的姿势避免路由的增长(包括RouteAttribute注册的路由)?

    按照 Visual Studio 给出的 WebAPI 路由和 ApiController 示例
    要怎么实现如下获取客户开通的服务?

    1、获取指定服务

    请求方式&URL地址:[GET] http://*/api/service/服务编号

    2、获取指定客户的所有服务

    请求方式&URL地址:[GET] http://*/api/service/客户编号

    难道只能这样写吗?

        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) {...}
        }

    如果按照这种思路那岂不是要注册非常多的路由?

    那么加入一个新API呢?

    3、获取指定服务以及个性化配置值

    请求方式&URL地址:[GET] http://*/api/service/full/服务编号
    难道只能这样写吗?

        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) {...}
        }

    疑惑2

    按照网上RESTful介绍我的第三个API的URL地址应该是(/api/service/服务编号/config 或 /api/service/服务编号/config/配置编号)
    针对这样的URL方法签名该怎么写呢?
    难道这样写?

        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) {...}
        }

    我对.Net WebAPI 的了解仅是皮毛而已,在此我把我目前使用的解决方案也写出来,请大神指教。

    我目前的解决方案

    我目前使用的解决方案是GET请求的参数都放到"?"后面,URL参数名和Action的参数名必须一致。自己实现支持命名空间的ControllerSelector。

    路由注册代码如下:

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

    请求的URL地址如下:
    不知道还算不算是RESTful风格了。。。

    1、获取指定服务
    /api/unit/service/single?serviceID=服务编号
    其中unit是命名空间名。

    2、获取指定客户的所有服务
    /api/unit/service/list?unitid=客户编号
    其中unit是命名空间名。

    3、获取指定服务以及个性化配置值
    /api/unit/service/complete/single?serviceID=服务编号
    其中unit和service是命名空间名。

    Controllers代码如下:

    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) {...}
        }
    }
    0
    打赏
    收藏
    点击回答
        全部回答
    • 共 0 条
    • 只有回憶而已丶 普通会员 1楼

      RESTful的.net webapi路由和URL在实际开发中是非常重要的,但有时会让人感到困惑。以下是一些关于RESTful的.net webapi路由和URL的疑问:

      1. 什么是RESTful的.net webapi路由? RESTful的.net webapi路由是一种设计原则,它将URL映射到HTTP方法(如GET,POST,PUT,DELETE)和URI(统一资源标识符)。例如,如果你有一个“/users”URL,那么你应该将请求发送到GET方法,并将请求体中的“id”作为URI。

      2. 什么是RESTful的.net webapi URL? RESTful的.net webapi URL是URL的组成部分,它通常以“/”开始,并包括一个唯一的标识符(或ID)和一个有效的HTTP方法。例如,一个URL可能是“/users/123”,其中“123”是标识符,表示这个用户。

      3. RESTful的.net webapi路由和URL有什么区别? RESTful的.net webapi路由和URL在设计原则和实现方式上有很大的区别。RESTful的.net webapi路由更加注重资源的统一管理,它将资源的路径和HTTP方法作为一个基本单位进行设计和实现。而RESTful的.net webapi URL则更注重URL的简洁性和易读性。

      4. 如何在RESTful的.net webapi路由中使用URL? 在RESTful的.net webapi路由中,你可以使用URI作为URL的一部分,也可以使用相对路径或绝对路径作为URI。例如,你可以使用URI作为URL的一部分来访问“/users/123”,也可以使用相对路径或绝对路径来访问“/users/123?name=John”。

      总的来说,RESTful的.net webapi路由和URL是一个强大的工具,可以帮助你更有效地管理和访问资源。在使用时,你需要根据实际需求来选择合适的URI和HTTP方法。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部