简体   繁体   English

ASP.NET Core 路由返回 404

[英]ASP.NET Core routing return 404

I am creating a web api using ASP.NET Core 3.1 and am trying to route URL to controllers.我正在使用 ASP.NET Core 3.1 创建一个 web api,并试图将 URL 路由到控制器。 So far I have a basic controller like this:到目前为止,我有一个这样的基本控制器:

    [Route("abc")]
    [ApiController]
    public class ABCController : ControllerBase
    {
        // GET: abc/1234
        [HttpGet("{id}")]
        public async Task<ActionResult<string>> GetABCService(long id)
        {
              ...
        }
    }

Which correctly route me to the page when I type in http://myurl/abc/1234.当我输入 http://myurl/abc/1234 时,哪个正确地将我路由到页面。 The next thing I controller I wanted to wire is like this:接下来我要连接的控制器是这样的:

    [Route("xxx")]
    [ApiController]
    public class XXXController : ControllerBase
    {

        // GET: abc/1234/XXX
        [HttpGet("{id}")]
        public async Task<ActionResult<string>> GetXXXService(long id)
        {
              ...
        }
    }

Somehow it keeps giving me 404 when I type in http://myurl/abc/1234/xxx.不知何故,当我输入 http://myurl/abc/1234/xxx 时,它一直给我404 I made the first one works by setting my endpoint like this :我通过像这样设置我的端点来使第一个工作:

app.UseEndpoints(endpoints =>{
                endpoints.MapControllerRoute(
                    "abc",
                    "abc/{id}",
                    new {controller = "ABCController", action = "GetABCService"});

              //My current endpoint mapping for the second controller:
                endpoints.MapControllerRoute(
                    "xxx",
                    "abc/{id}/xxx",
                    new {controller = "XXXController", action = "GetXXXStatus" });
}

I could not figure out why I would get 404 with http://myurl/abc/1234/xxx.我不明白为什么我会用 http://myurl/abc/1234/xxx 得到404 Any insight?任何见解?

You want to say XXXController to route 'abc' first by [Route("abc")]你想说XXXController首先通过[Route("abc")]路由 'abc'

[Route("abc")]
    [ApiController]
    public class XXXController : ControllerBase
    {
        [HttpGet("{id}/xxx")]
        public ActionResult<string> GetXXXService(long id)
        {
            return "ActionResult";
        }
       
    }

When you use attribute routing, eg with [Route] or [HttpGet(…)] , then convention-based routing is ignored.当您使用属性路由时,例如使用[Route][HttpGet(…)] ,则基于约定的路由将被忽略。 So the route templates you define with MapControllerRoute are not taken into account when generating the routes for your API controller.因此,在为 API 控制器生成路由时,不会考虑使用MapControllerRoute定义的路由模板。 In addition, using the [ApiController] attribute actually enables certain API-related conventions.此外,使用[ApiController]属性实际上会启用某些与 API 相关的约定。 And one of those convention is that you may only use attribute routing for your API controllers.其中一项约定是您只能为 API 控制器使用属性路由。

So if you only have API controllers in your project, then you can leave out the MapControllerRoute calls.因此,如果您的项目中只有 API 控制器,那么您可以省略MapControllerRoute调用。 Instead, you will have to make sure that your attribute routing is correct.相反,您必须确保您的属性路由是正确的。

In your case, if you want the route abc/1234/XXX to work, then you will have to use a route of abc/{id}/XXX .在您的情况下,如果您希望路线abc/1234/XXX起作用,那么您将不得不使用abc/{id}/XXX路线。

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

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