asp.net-coreasp.net-core-routing

asp.net core route handle id after domain


How can I handle this in the ASP.NET Core route http://example.com/id?

Basically, I need to be able to format some URL with some kind of id right after the domain (please notice this is not /Home/Index).

So assuming I want to display an image with id=1 when you visit the url: http://example.com/1 or http://example.com?id=1 where can I put my logic? How can I handle this route?


Solution

  • This is basic routing, and the docs answer for you how to do this.

    To strictly handle http://example.com/{id}, you need to define a default controller (typically HomeController), and define a route that matches the id.
    Inside HomeController would be an action that looks something like

    [HttpGet("{id}")]
    public void GetImage(string id) {
        ...
    }