0

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

0

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

actually /api/v1/Admin/Keys/GetAllKeys is exactly what I want but I cant figure out why it is not working. url is "localhost:60907/api/v1/Admin/Keys/AllKeys" but I am geting: "This localhost page can’t be found"
@ThunD3eR you need to call [..]/GetAllKeys, not [..]/AllKeys
I've updated my answer. Set the route on the action using the HttpGet attribute, not Route.
Sorry m8, the image was a bit off. I am calling GetAllKeys
I tested this out in one of my apps. But note that in my Startup.cs, I have app.UseMvc() (no parameters).
|
1

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()
   { 
      ... 
   }
}

Comments

0

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.

1 Comment

Sounds right. Two applications can't bind to the same TCP port. If you want them working under the same port, you'd have to use a web server (like IIS) as a proxy so that the root of the domain forwards the request to your front end, and /api forwards to your API application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.