C# ASP.NET Web API路由未映射到控制器操作

C# ASP.NET Web API路由未映射到控制器操作,c#,asp.net-mvc,asp.net-web-api,C#,Asp.net Mvc,Asp.net Web Api,我有一个ASP.NET Web API应用程序,希望在其中响应以下路径: 1. http://localhost:1234/values 2. http://localhost:1234/values/123 3. http://localhost:1234/values/large 4. http://localhost:1234/values/small 注:这些路线和示例仅为示例。但它们映射到我想要实现的目标 应该返回所有值,比如一个数字列表 应返回id为123的资源的值 应返回应用程序

我有一个ASP.NET Web API应用程序,希望在其中响应以下路径:

1. http://localhost:1234/values
2. http://localhost:1234/values/123
3. http://localhost:1234/values/large
4. http://localhost:1234/values/small
注:这些路线和示例仅为示例。但它们映射到我想要实现的目标

  • 应该返回所有值,比如一个数字列表
  • 应返回id为123的资源的值
  • 应返回应用程序认为较大值的内容列表。不管是什么
  • 应该返回应用程序考虑小值的列表。 与10亿ASP.NET Web Api路由示例一样,数字(1)和(2)非常简单。但当我尝试解决(3)和(4)时,那么(1)和(2)就不再有效了

    这是我目前的路线:

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
            // trying to map to "values/large" or "values/small"
            routes.MapHttpRoute(
                name: "ActionsApi",
                routeTemplate: "{controller}/{action}",
                defaults: null,
                constraints: new { action = @"^[a-zA-Z]+$" }
            );
    
            // trying to map to "values/123"
            routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "{controller}/{id}",
                defaults: null,
                constraints: new { id = @"^\d+$" }
            );
    
            // trying to map to "values"
            routes.MapHttpRoute(
                name: "ControllerApi",
                routeTemplate: "{controller}"
            );
    
    通过上述路线,(3)和(4)工作

    (2) 返回:

    我在此处添加的可选默认值:

            routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "{controller}/{id}",
                defaults: new { id = RouteParameter.Optional },
                constraints: new { id = @"^\d+$" }
            );
    

    您是否考虑过将路由保留为默认路由,并处理控制器中的“值”

    e、 g。 更改Get以使id为string类型,然后检查控制器中的“特殊”值

    public class ValuesController : ApiController
        {
    
        // GET api/values/5
        public string Get(string id)
        {
            if (id == "large")
            {
                return "large value";
            }
    
            if (id == "small")
            {
                return "small value";
            }
    
            return "value " + id;
        }
    }
    

    您是否考虑过将路由保留为默认路由,并处理控制器中的“值”

    e、 g。 更改Get以使id为string类型,然后检查控制器中的“特殊”值

    public class ValuesController : ApiController
        {
    
        // GET api/values/5
        public string Get(string id)
        {
            if (id == "large")
            {
                return "large value";
            }
    
            if (id == "small")
            {
                return "small value";
            }
    
            return "value " + id;
        }
    }
    

    123
    看起来它与regex
    ^\w+$
    @davintroon哦!我真傻!我认为\w只匹配字母(如单词)。还包括数字。谢谢你,David。
    123
    看起来它与regex
    ^\w+$
    @davintroon匹配哦!我真傻!我认为\w只匹配字母(如单词)。还包括数字。谢谢你,大卫,谢谢。我将使用这个方法,然后根据id的值调用工作函数——不管是int,还是像“大”或“小”这样的字符串。谢谢。我将使用这个方法,然后根据id的值调用工作函数——不管它是int还是像“large”或“small”这样的字符串。
            routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "{controller}/{id}",
                defaults: new { id = RouteParameter.Optional },
                constraints: new { id = @"^\d+$" }
            );
    
    public class ValuesController : ApiController
        {
    
        // GET api/values/5
        public string Get(string id)
        {
            if (id == "large")
            {
                return "large value";
            }
    
            if (id == "small")
            {
                return "small value";
            }
    
            return "value " + id;
        }
    }