ThunD3eR
ThunD3eR

Reputation: 3456

.net core routing via controller attribute

I have a controller that gets a route attribute:

[Route("api/v1/Admin/Keys")]
public class AdminController : Controller
{}

My Webproject has the following routes:

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Admin}/{action=GetAllKeys}/{id?}");
});

Error:

This localhost page can’t be found No web page was found for the web address: http://localhost:60907/

When trying to go call a method on the controller which is also routed I keep getting the same.

If I remove the route attribute from the controller then this starts to work but I do not get the desired route.

What am I doing wrong?

EDIT:

Method that I am calling in the browser:

    [HttpGet]
    [Route("GetAllKeys")]
    public async Task<IActionResult> GetAllKeys()
    {
        var data = await _manager.GetAllKeyTypes();

        return Ok(data);

    }

error:

This localhost page can’t be found No web page was found for the web address: http://localhost:60907/

EDIT2:

Now when removing the global route and just using the:

  [HttpGet("GetAllKeys")]

And using the following url: http://localhost:60907/GetAllKeys

This works but again it is not desiered

Upvotes: 0

Views: 168

Answers (3)

ThunD3eR
ThunD3eR

Reputation: 3456

Alright...

Note to self...do not modify ports in .net core. I was experimenting with my API and my front-end project. Tried to put them on the same port but with a different access point. It worked as I thought which was that while the API was running then the node server could not start on the same port.

This however caused some kind of change somewhere which later when I changed back to the default port on the API never fully registered. I checked the launchSettings.json and modified it accordingly but still this did not work.

Final solution: Removed the project from my pc and got it back from my repo and now it works as it should with the original routes set.

Upvotes: 0

Gabriel Luci
Gabriel Luci

Reputation: 40928

If you only have [Route("api/v1/Admin/Keys")] applied to the controller, then you would have to access the GetAllKeys action using /api/v1/Admin/Keys/GetAllKeys. I don't think that's what you want.

Try this:

[Route("api/v1/Admin/Keys")]
public class AdminController : Controller {

    [HttpGet("GetAllKeys")]
    public async Task<IActionResult> GetAllKeys()
    {
        var data = await _manager.GetAllKeyTypes();

        return Ok(data);

    }    
}

That tells it that every action in the AdminController is under /api/v1/Admin/Keys. Then the HttpGet attribute tells it that GetAllKeys should be available via /api/v1/Admin/Keys/GetAllKeys.

Once you set Route on the controller, it seems you have to use HttpGet to set the route on the action, otherwise it just doesn't work.

Upvotes: 2

Set
Set

Reputation: 49779

Convention routing is not used for controllers/actions with attribute routing configuration.

Attribute routing allows defining more than one route for the same action. You could simply do somehting like this:

public class AdminController : Controller
{
   [Route("")]
   [Route("Admin")]
   [Route("Admin/GetAllKeys")]
   [Route("api/v1/Admin/Keys")]
   public IActionResult GetAllKeys()
   { 
      ... 
   }
}

Upvotes: 1

Related Questions