2的WebAPI与属性方面不能正常工作的路由 [英] WebAPI 2 Attribute routing with areas not working

查看:464
本文介绍了2的WebAPI与属性方面不能正常工作的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了麻烦的WebAPI 2属性的路由工作。我在寻找路由方案是 / API / {产品} / {控制器} / {ID-可选} ,所以像 / API /愿景/ IdCard 。该控制器是在一个地区,并设立这样的:

 命名空间DataServices.Controllers.Vision
{
     [途径preFIX(API /愿景)]
        公共类IdCardController:BaseApiController
        {
            [System.Web.Mvc.RequireHttps]
            〔路线(idcard)]
            公共IdCardViewModel获取(HTT prequestMessage要求)
            {...}

每当我做一个到达这个,我得到的 404 。我包括在该命名空间的区域是在它自己的命名空间。是的WebAPI属性路由支持的领域?

编辑:WebApiConfig看起来是这样的:

  CONFIG.SUP pressDefaultHostAuthentication();
            config.Filters.Add(新HostAuthenticationFilter(OAuthDefaults.AuthenticationType));            //网页API路线
            config.MapHttpAttributeRoutes();            config.Routes.MapHttpRoute(
                名称:DefaultApi
                routeTemplate:API / {}控制器/(编号),
                默认:新{ID = RouteParameter.Optional}
            );


解决方案

区功能在Asp.Net的Web API项目中不存在,它很难像基于命名空间的控制器定制的方式来维护。
我已经检查与基于控制器的命名和路由,如单操作方法的许多问题是基于命名空间的路由以及默认routing.Thus自定义实现访问并不能减轻我们的要求。

要解决这个问题,我们可以用简单的方式来管理控制器路由为:

  //删除默认的路由注册
      /*config.Routes.MapHttpRoute(
            名称:DefaultApi
            routeTemplate:API / {}控制器/(编号),
            默认:新{ID = RouteParameter.Optional}
        ); * /

和只使用基于属性的路由而已,像

  [路线preFIX(API /用户/家庭)]
公共类UserHomeController:ApiController
{
     [路线]
     公共字符串获得()
     {
       返回测试用户GET;
     }
}

和针对不同地区/模块控制器

  [路线preFIX(API /管理/家)]
公共类AdminHomeController:ApiController
{
     [路线]
     公共字符串获得()
     {
       返回测试管理GET;
     }
}

这种方法的优点是:


  • 无需如基于空间区域,自定义路由处理程序自定义逻辑的,所以它能够更好地code方式。

  • 只需要在操作添加属性[路线]到可用性API

I'm having trouble getting WEBAPI 2 attribute routing to work. The routing scheme I'm looking for is /api/{product}/{controller}/{id-optional}, so like /api/Vision/IdCard. The controllers are in an area and are set up like this:

namespace DataServices.Controllers.Vision
{
     [RoutePrefix("api/vision")]
        public class IdCardController : BaseApiController
        {
            [System.Web.Mvc.RequireHttps]
            [Route("idcard")]
            public IdCardViewModel Get(HttpRequestMessage request)
            {...}

Whenever I do a get to this, I get a 404. I included the namespace as the area is in it's own namespace. Are areas supported in WEBAPI attribute routing?

EDIT: WebApiConfig looks like this:

 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 }
            );

解决方案

Area functionality not available in Asp.Net Web API project, and its harder to maintain with custom way like Namespace based controller. I have checked many problems with namespace based controller and routing, like single action method is accessible by namespace based routing as well as default routing.Thus custom implementation does not mitigate our requirements.

To resolve this issue we can use simple way to manage controllers routing as :

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

And use just attribute based routing only, like

[RoutePrefix("api/user/home")]
public class UserHomeController : ApiController
{
     [Route]
     public string Get()
     {
       return "Test user GET";
     }
}

And for different area/module controller

[RoutePrefix("api/admin/home")]
public class AdminHomeController : ApiController
{
     [Route]
     public string Get()
     {
       return "Test admin GET";
     }
}

Advantages with this approach are:

  • No need of custom logic like namespace based area, custom routing handler, so its better way to code.
  • Just need to add attribute [Route] in action to availability in API

这篇关于2的WebAPI与属性方面不能正常工作的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆