【问题标题】:Cannot config or rewrite routing in ASP.NET MVC Web Api无法在 ASP.NET MVC Web Api 中配置或重写路由
【发布时间】:2016-09-28 01:23:17
【问题描述】:

我正在开发一个 ASP.NET MVC Web Api。我正在为我的 api 重写路由以使 url 整洁。但它不起作用。

我在 ItemsController 中有这样的操作方法:

public HttpResponseMessage Get([FromUri]string keyword = "", [FromUri]int category = 0, [FromUri]int region = 0, [FromUri]int area = 0, [FromUri]int page = 0,[FromUri]int count = 0)
{
.
.
.
}

在 WebApiConfig 中,我为该操作配置路由,如下所示:

  config.Routes.MapHttpRoute(
            name: "",
            routeTemplate: "api/v1/places/{category}/{region}/{area}/{page}/{count}",
            defaults: new { controller = "ItemsController" , keyword = "" , category = 0 , region = 0 ,area = 0 , page = 0 , count = 0 }
        );

如您所见,我没有在路由中设置关键字。但是当我从下面的 url 访问时,它给了我错误。

这就是我提出获取请求的方式:

http://localhost:50489/api/v1/places/0/0/0/1/2

这是错误:

{
    "Message": "No HTTP resource was found that matches the request URI 'http://localhost:50489/api/v1/places/0/0/0/1/2'.",
    "MessageDetail": "No type was found that matches the controller named 'ItemsController'."
}

我该如何重写它?我想在该网址中排除关键字。我也会有另一条路线来执行该操作。

这也不好用:

config.Routes.MapHttpRoute(
                name: "",
                routeTemplate: "api/v1/places/{keyword}/{category}/{region}/{area}/{page}/{count}",
                defaults: new { controller = "ItemsController" , keyword = "" , category = 0 , region = 0 ,area = 0 , page = 0 , count = 0 }
            );

【问题讨论】:

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


    【解决方案1】:

    该错误表明您的控制器不存在。

    这是因为 Web API 正在寻找一个名为 ItemsControllerController 的控制器。后缀Controller 由框架自动添加。所以,如果你的控制器实际上被命名为ItemsController,你的路由应该是:

    config.Routes.MapHttpRoute(
            name: "",
            routeTemplate: "api/v1/places/{category}/{region}/{area}/{page}/{count}",
            defaults: new { controller = "Items" , keyword = "" , category = 0 , region = 0 ,area = 0 , page = 0 , count = 0 }
        );
    

    【讨论】:

      【解决方案2】:

      下面的虚拟查询尝试使用这种方式..只需进行相应的更改

      API 代码:

      [Route("api/{Home}/{Username}/{Password}")]
              public HttpResponseMessage Get(string Username, string Password)
              {
      //code here
      }
      

      WebApiConfig

      public static void Register(HttpConfiguration config)
              {
                  // Web API configuration and services
                  // Configure Web API to use only bearer token authentication.
                  config.SuppressDefaultHostAuthentication();
                  config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
      
                  // Web API routes
                  config.MapHttpAttributeRoutes();
      
      
                  config.Routes.MapHttpRoute(
                   name: "DefaultApi",
                   routeTemplate: "api/{controller}/{id}",
                   defaults: new { id = RouteParameter.Optional }
               );
      
                  config.Routes.MapHttpRoute(
                  name: "ContactApi",
                  routeTemplate: "api/{controller}/{Username}/{Password}"
                  );
      
      
              }
      

      【讨论】:

        【解决方案3】:

        如果您使用的是 ASP.NET Web API 2,那么您可以使用@stylishCoder Attribute Routing in ASP.NET Web API 2 提供的代码 sn-p。

        这个link 将帮助您将多个对象传递到一个端点。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-01-19
          • 2017-03-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-12
          • 2021-04-23
          • 1970-01-01
          • 2012-10-03
          相关资源
          最近更新 更多