1

I have this routing in program class in ASP.NET Core 6:

app.MapControllerRoute(
    name: "default",
    pattern: "{action}/{id}/{Title?}"
);

In my controller I have this action :

public IActionResult Index(int id)
{
    string title = get-Item-Title-From-Database-By-Id(id);
    return View();
}

I want show title in URL, like Stackoverflow

https://stackoverflow.com/questions/questionId/questionTitle

Is there a way to do this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
payam purchi
  • 226
  • 1
  • 11

1 Answers1

1

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);

enter image description here

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>
}

enter image description here

enter image description here

Tiny Wang
  • 10,423
  • 1
  • 11
  • 29
  • yes .that's worked but when user enter questionId in URL , The title of the question is displayed on URL , How to put the title of the question in the URL ??? – payam purchi Jul 17 '23 at 09:19
  • do you mean, users enter `/question/id` in the browser and click enter, then the page would load and the url would change to `/question/id/title`? – Tiny Wang Jul 17 '23 at 09:23
  • if so, maybe you could add a redirect in the controller, like this `if (title == null) {return LocalRedirect("/Data/Index/id/title");}` – Tiny Wang Jul 17 '23 at 09:31
  • I mean, the first time user hit `/question/id`, then in my sample controller, title is null, then we could add if clause and use id to query the title result, then have a redirection with url `/question/id/title`. This time the title is inside the url. I haven't test it yet.. one more minute – Tiny Wang Jul 17 '23 at 09:33
  • yes it worked for me. – Tiny Wang Jul 17 '23 at 09:34
  • Yes that's right . But the problem here is that we have to search the database twice. Once to get the title and redirect to another controller and once to use in the view – payam purchi Jul 17 '23 at 09:42
  • is it possible for you to redirect with the data you queried from the database? Or storing the data in session? Or memory database like Redis? – Tiny Wang Jul 17 '23 at 09:45
  • I don't think this is a good idea. In my opinion, .NET Core should have a feature for these situations. Don't you think so? – payam purchi Jul 17 '23 at 09:56
  • I think.... ok pls try this idea, I mean when users hit `question/id`, then in the controller you need to query the title if it doesn't contain the title, then after you queried the database, you might have much data including the title and want to render the page, upon rendering the page, you could use js code to change the url and make the url containing the title. – Tiny Wang Jul 17 '23 at 10:11
  • No That's not true – payam purchi Jul 17 '23 at 10:55
  • ok, I don't have any other ideas then. sorry for it. – Tiny Wang Jul 17 '23 at 10:56