【问题标题】:WebApi actionmethod with string parameter notFound带有字符串参数 notFound 的 WebApi 操作方法
【发布时间】:2017-06-23 13:12:24
【问题描述】:

我的控制器中有两个 Get 方法,其中一个带有两个字符串参数,第二个带有一个字符串参数,但是当我使用一个字符串参数调用方法时,它返回 404,而带有两个字符串参数的方法有效美好的。我认为我的路由有问题。

RouteConfig.cs:

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
            routes.MapHttpRoute("Login", "api/{controller}/{email}/{password}",
                                new
                                {
                                    email = UrlParameter.Optional,
                                    password = UrlParameter.Optional
                                });

WebApiConfig.cs:

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

控制器:

[HttpGet]
        public IHttpActionResult Validate(string id)
        {
           //Some code here // doesn't work
        }

        [HttpGet]
        public IHttpActionResult GetCitizen(string email, string password)
        {
            //Some code here //works fine
        }

【问题讨论】:

    标签: asp.net-web-api asp.net-mvc-routing


    【解决方案1】:

    你可以试试属性路由吗?就像,就在您的验证方法上方添加属性

    [Route("api/<controllername>/Validate")]
    

    打电话时你可以打电话

    http://<localhost>:<port>/api/<controllername>/validate?id=123
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-16
      • 1970-01-01
      • 2014-05-29
      • 2014-03-03
      相关资源
      最近更新 更多