【问题标题】:How to configure the url with a combination of url segmentation and querystring for web api rest url如何结合使用 url 分段和查询字符串为 web api rest url 配置 url
【发布时间】:2016-09-16 23:09:39
【问题描述】:

(使用 Web api 1.0) 我有一个场景,我的动作有多个参数,比如 GetCustomer(int id,字符串电子邮件) 其中 url 将被指定为

获取 api/客户/{id}/{email}

但我希望将网址配置为

api/Customer/{id}?email={email}

因此,如果 url 分段与查询字符串相结合。目前,当我尝试设置它时,我收到以下错误。

路由 URL 不能以“/”或“~”字符开头,并且不能包含“?”特点。 参数名称:routeUrl

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

        routes.MapRoute(
         "Help Area",
         "",
         new { controller = "Help", action = "Index" }
     ).DataTokens = new RouteValueDictionary(new { area = "HelpPage" });

    }
}

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {                                                                             

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

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

请提前帮我解决这个问题

【问题讨论】:

  • 你有没有尝试使用 Route 属性?
  • 展示你如何配置路由。
  • @FelipeOriani 我刚刚在问题中更新了我的路线配置,如果您需要更多信息,请告诉我,谢谢您的帮助
  • @Nkosi 我刚刚在问题中更新了我的路线配置,如果您需要更多信息,请告诉我,谢谢您的帮助
  • 您说的是 web api,但您显示的路线是针对 MVC 而不是 web api。所以不清楚你指的是什么

标签: c# asp.net-web-api asp.net-web-api-routing


【解决方案1】:

您的配置需要更新

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config) {

      config.Routes.MapHttpRoute(
          name: "CustomerApi_GetCustomer",
          routeTemplate: "api/Customer/{id}",
          defaults: new { controller = "Customer" action = "GetCustomer" }
      );

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

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

如果您向api/Customer/{id}?email={email}提出请求

活页夹会自动匹配GetCustomer(int id, string email) 与来自url 的email 参数

【讨论】:

  • 我看到在放置您的代码后 api/Customer?id={id}&email={email} 正在工作,但没有 api/Customer/{id}?email={email} throwing No action was在与请求匹配的控制器“api”上找到。
  • 你按我显示的顺序放了吗?因为在设置路线时顺序很重要。
  • 是的,我已经给出了确切的顺序,但我收到错误“在与请求匹配的控制器 'api' 上找不到任何操作。”
  • 看起来你的 web api 配置没有被调用。你在哪里打电话WebApiConfig.Register。检查启动。还要检查您的控制器并确保它继承自 ApiController 而不是 Controller
  • WebApiConfig.Register(GlobalConfiguration.Configuration);从 Global.ascx 调用,它肯定在工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-19
  • 1970-01-01
  • 2021-05-30
  • 1970-01-01
  • 2016-07-05
  • 2021-01-06
  • 1970-01-01
相关资源
最近更新 更多