if you want to have similar feature like Stackoverflow, you can see that the url it provided already containing the title, rather than what you write string title = get-Item-Title-From-Database-By-Id(id);

What you should do is querying the database ahead to generate the url which containing the questionId and questionTitle
, then change your template to
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}/{title?}");
so that we could have an <a href="/question/id/title"></a>
to let users click the link and our controller could handle the request.
public IActionResult Index(string id, string? title)
{
UserModel model = new UserModel {
id = 1
};
ViewBag.title = "{controller}/{action}/"+id+"/my-title";
return View(model);
}
@section Scripts{
<script>
$(document).ready(function () {
alert("@ViewBag.title");
window.history.pushState("object or string", "Title", "/@ViewBag.title");
});
</script>
}

