简体   繁体   English

应用程序区域中的ASP.NET MVC WebAPI

[英]ASP.NET MVC WebAPI in Application Area

I am trying to use WebAPI as part of an MVC5 application. 我正在尝试将WebAPI用作MVC5应用程序的一部分。 The application has areas, so to keep with the existing structure I have created and "API" area. 该应用程序具有区域,因此与我创建的现有结构和“ API”区域保持一致。 However, I am having some routing troubles as I get a 404 when hitting the API URL. 但是,当我点击API URL时收到404错误,因此遇到了一些路由问题。

I have read a number of posts on this, mostly MVC4 examples and so far I have not had any success. 我已经阅读了许多有关此示例的文章,其中大部分是MVC4示例,到目前为止,我还没有取得任何成功。

Initially, I had the API routes as part of the area routing, so above the below default route, I had another with the RouteTemplate API/{controller} 最初,我将API路由作为区域路由的一部分,因此在下面的默认路由之上,我使用了RouteTemplate API/{controller}

public override void RegisterArea(AreaRegistrationContext context)
{
     context.MapRoute(
          "API_default",
          "API/{controller}/{action}/{id}",
          new { action = "Index", id = UrlParameter.Optional }
      );
}

I then realised my error and moved the API route to a different method in the same area registration file 然后,我意识到自己的错误,并将API路由移至同一区域注册文件中的其他方法

public static void Register(HttpConfiguration config)
{
     config.Routes.MapHttpRoute(
         name: "Direct_API",
         routeTemplate: "API/{controller}"
     );
}

I have now moved the API routes out of the area registration file completely, created a new API route config file in app_start and registered it in global.asax before the application routes file. 现在,我已将API路由完全移出区域注册文件,在app_start创建了一个新的API路由配置文件,并在应用程序路由文件之前将其注册到global.asax However, still, if I goto http://localhost:port/API/Import I do not hit the API Controller. 但是,如果我转到http://localhost:port/API/Import ,则不会点击API控制器。

The API Import Controller sits in the API area of the application API导入控制器位于应用程序的API区域中

public class ImportController : ApiController
{
   [HttpGet]
   public Array Get()
   {
        return new[]{true, false, false, true};
   }
}

Global.asax registration Global.asax注册

AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

I have also tried registering the file MVC4 way which was WebApiConfig.Register(GlobalConfiguration.Configuration); 我也尝试过注册文件MVC4方式,即WebApiConfig.Register(GlobalConfiguration.Configuration);

I found the answer to this but think I will leave the question up to help others in the future. 我找到了答案,但我认为将来我会把这个问题留给别人帮助。 Many of the examples and articles covering this subject and very specific that the RouteConfig must be after the WebAPI config. 许多涉及此主题的示例和文章都非常明确, RouteConfig 必须位于WebAPI配置之后。 I was very careful about this. 我对此非常小心。

However, I completely overlooked that area registrations were running first. 但是,我完全忽略了先进行区域注册。 Moving AreaRegistration.RegisterAllAreas(); 移动区域AreaRegistration.RegisterAllAreas(); to after GlobalConfiguration.Configure(WebApiConfig.Register); GlobalConfiguration.Configure(WebApiConfig.Register); solved the issue. 解决了这个问题。

This still leaves the API registration global, which doesn't feel right as its localised to an area. 这仍然使API注册成为全局,这在本地化到某个区域时感觉不对。 I think i can solve that with attribute routing and the removal of the Area Registration File for the API. 我想我可以通过属性路由和删除API的区域注册文件来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM