简体   繁体   English

除非以特定方式命名控制器类,否则WebApi2属性路由将不起作用

[英]WebApi2 Attribute Routing doesn't work unless controller classes are named in a specific manner

I was debugging an issue with attribute routing giving 404s for known-good routes. 我正在调试有关属性路由的问题,该问题为已知有效的路由提供了404。 Well, known-good to me, but they weren't being picked up by the framework. 好吧,对我来说很好,但是框架并没有将它们接受。 I was grouping my controllers/models into logical units, eg: 我将控制器/模型分为逻辑单元,例如:

Auth
    Models
        Model1
        Model2
    Controller

So my "AuthController" is named MyApi.Auth.Controller instead of, for instance, MyApi.Auth.AuthController . 因此,我的“ AuthController”被命名为MyApi.Auth.Controller而不是例如MyApi.Auth.AuthController

My routes were not being picked up until I renamed my controller, adding anything before it, eg zzController . 直到我重命名控制器,并在它之前添加任何内容,例如zzController ,我的路由才被zzController This makes me think there's a bug in attribute routing, or am I missing something? 这使我认为属性路由存在错误,还是我错过了一些东西?

ASP is convention over configuration. ASP是配置之上的约定。 Whether they add features that go away from that or not, always stick to the convention when it comes to Controllers and Views. 无论它们是否添加了其他功能,在Controllers和View时始终遵循约定。 It'll make maintenance so much easier (plus in the next version of MVC, there is no more ApiController, everything is just a Controller). 这将使维护变得非常容易(在下一版本的MVC中,再也没有ApiController了,一切都只是一个Controller)。

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

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