0
votes

I Have a problem configuring routes in ASP Core.

In Startup.cs I used default configuration: services.AddMvc() in ConfigureServices() and app.UseMvc() in Configure().

Now I have a simple controller in the same assembly:

[Route("/api/[controller]")]
public class TestController: Controller
{
    [HttpGet]
    public string Test()
    {
        return "Hello";
    }
}

Request /api/test/test doesn't fire

But if i add [HttpGet("test")] or [Route("test")] it works well.

However I'd like to support convention over configuration in case route attribute is not specified

3

3 Answers

3
votes

Try using:

[Route("api/[controller]/[action]")]
0
votes

When you add [Route("/api/[controller]")] annotation to Controller, the default routing configured in Startup.cs will ignore this Controller.

So you either need to specify URL suffix for each of your actions inside that Controller by adding [Route("")] attribute above them:

[Route("/api/[controller]")]
public class TestController: Controller
{
    [Route("test")]
    [HttpGet]
    public string Test()
    {
        return "Hello";
    }
}

Or specify in Controller annotaion that the routing should use action names as part of URL :

[Route("/api/[controller]/[action]")]
public class TestController: Controller
{
    [HttpGet]
    public string Test()
    {
        return "Hello";
    }
}
0
votes
     [Route("/api/[controller]")]
 public class TestController: Controller
 {
    [HttpGet(namOf(Test),Name=namOf(Test))]
    public string Test()
    {
        return "Hello";
    }
 }