【问题标题】:ASP.Net WebAPI Routing ConfigurationASP.Net WebAPI 路由配置
【发布时间】:2017-08-05 00:06:02
【问题描述】:

我已经定义了以下路线:

GlobalConfiguration.Configuration.Routes.Add(
    "iOS Service",
    new HttpRoute("ios/{controller}/{action}/{id}", new HttpRouteValueDictionary { { "id", RouteParameter.Optional } })
);

GlobalConfiguration.Configuration.Routes.MapHttpRoute(
    name: "iOS Service Documents",
    routeTemplate: "ios/getfulldocumentstructure",
    defaults: new { controller = "Documents", action = "GetFullDocumentStructure" }
);

GlobalConfiguration.Configuration.Routes.MapHttpRoute(
    name: "iOS Service AppInfo",
    routeTemplate: "ios/appinfo",
    defaults: new { controller = "AppInfo", action = "GetAppInfo" }
);

GlobalConfiguration.Configuration.Routes.MapHttpRoute(
    name: "iOS Service Html",
    routeTemplate: "ios/html/{language}/{contentId}",
    defaults: new { controller = "Html", action = "GetHtml", language = RouteParameter.Optional, contentId = RouteParameter.Optional }
);

使用以下控制器:

public class HtmlController : ApiController
{
    [HttpGet]
    public string GetHtml(long language, long contentId)
    {

        return "Hello";
    }
}

如果我使用http://localhost/ios/html?languageId=1033&contentId=12345 访问服务,GetHtml 操作就会触发。

如果我使用 http://localhost/ios/html/1033/12345 访问服务,我会收到一个错误,即在控制器中找不到匹配的操作。

我做错了什么?

【问题讨论】:

  • 您是否定义了任何其他可能导致冲突的路由?这条路线相对于其他路线是在哪里定义的?
  • 我定义了其他三个路由,但错误消息说它击中了正确的控制器 { "Message": "No HTTP resource was found that match the request URI 'localhost/ios/html/1033/2'.", "MessageDetail" :“在控制器 'Html' 上找不到与名称 '1033' 匹配的操作。” }
  • 仅仅因为它击中了正确的控制器并不意味着它击中了正确的路线。请发布您的其他路线及其注册顺序。
  • 带有完整路线列表的已编辑问题
  • 一方面,{language} 与您的 languageId 参数不匹配,因为一个具有 Id 后缀,而另一个则没有。

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


【解决方案1】:

如果您的参数是可选的,那么不妨试试这个:

public class HtmlController : ApiController
{
    [HttpGet]
    public string GetHtml(long? languageId, long? contentId)
    {

        return "Hello";
    }
}

【讨论】:

    【解决方案2】:

    将捕获所有路线移动到最后解决了这个问题。调用打错了控制器:

    GlobalConfiguration.Configuration.Routes.MapHttpRoute(
        name: "iOS Service Documents",
        routeTemplate: "ios/getfulldocumentstructure",
        defaults: new { controller = "Documents", action = "GetFullDocumentStructure" }
    );
    
    GlobalConfiguration.Configuration.Routes.MapHttpRoute(
        name: "iOS Service AppInfo",
        routeTemplate: "ios/appinfo",
        defaults: new { controller = "AppInfo", action = "GetAppInfo" }
    );
    
    GlobalConfiguration.Configuration.Routes.MapHttpRoute(
        name: "iOS Service Html",
        routeTemplate: "ios/html/{language}/{contentId}",
        defaults: new { controller = "Html", action = "GetHtml", language = RouteParameter.Optional, contentId = RouteParameter.Optional }
    );
    
    GlobalConfiguration.Configuration.Routes.Add(
        "iOS Service",
        new HttpRoute("ios/{controller}/{action}/{id}", new HttpRouteValueDictionary { { "id", RouteParameter.Optional } })
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-03
      • 1970-01-01
      • 1970-01-01
      • 2017-07-12
      相关资源
      最近更新 更多