marderWarner
marderWarner

Reputation: 23

Asp.net core how to get Id from url

Hay, I'm creating a website like wikipedia and I need to get the id from url to show correct view. This is my route config:

private void ConfigureRoutes(IRouteBuilder routeBuilder)
{
    routeBuilder.MapRoute(
        "Default",
        "{controller=Home}/{action=Index}/{id?}"
    );
}

The id will be name of the view that controller will return.

Upvotes: 2

Views: 7944

Answers (1)

Jonathan Wood
Jonathan Wood

Reputation: 67175

Where are you getting stuck? The default mapping that Visual Studio sets up for you supports this already. Just add an id parameter to your controller method and use it to look up your content.

public ActionResult Index(string id)
{
    if (id != null)
    {
        // Lookup topic from id
    }
}

Upvotes: 6

Related Questions