0

I'm using asp-route-id to pass the id value to the controller which will return me a view with information about "blog" with same id. Everything works but i don't like how "Url" looks!

How can I change "Url" like this https://localhost:5001/Blog/Blog/8 to this https://localhost:5001/Blog/Blog/title_of_blog ?

Details:

  1. there is a list of blogs blog/list

    html code for each of them:

    @model Blog <div class="col-12 col-md-6 col-lg-6" style="padding-top: 30px"> <div class="card card-shadow my-4 card_blog"> <img src="~/img/originals/img-09.jpg" class="card-img-top" alt="..."> <div class="card-body"> <h5 class="card-title">@Model.Title</h5> <p class="card-subtitle">@Model.Date.ToString("dd-MM-yyyy")</p> <p class="card-text">@Model.Description</p> <a asp-route-id="@Model.BlogID" asp-controller="Blog" asp-action="Blog" class="a_s_btn myBTN">Read More</a> </div> </div> </div>

  2. I'm clicking on tag <a asp-route-id="@Model.BlogID" asp-controller="Blog" asp-action="Blog" class="a_s_btn myBTN">Read More</a>

  3. here is the "Blog" action in the "Blog" cotroller

    public IActionResult Blog(int id) { var blogModel = new BlogsListViewModel(); blogModel.Blogs = repository.Blogs.OrderBy(p => p.BlogID).Where(p => p.BlogID == id); return View(blogModel); }

  4. The "Blog" action return to me a page with information about blog with "BlogID" = 8 blog page with own information

Futuricon
  • 1
  • 1
  • what is your `title_of_blog` in your model?And `asp-route-id` would generate the url like `https://localhost:5001/Blog/2 ` or `https://localhost:5001/Blog?id=2`,you could refer to [here](https://learn.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/anchor-tag-helper?view=aspnetcore-3.0#asp-route-value).How did you get the url like `https://localhost:5001/Blog/?id=2`?I suggest that you could share more details. – Rena Nov 11 '19 at 02:59
  • I added details to the question – Futuricon Nov 11 '19 at 17:52

1 Answers1

0

You can think of this problem in three parts:

1. Configure your website to handle new routing

In startup.cs you will find a section of the Configure method that is similar to:

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        };

Add a new route type to that ( but keep the existing route if you still want to use that for other places of the site.):

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "byBlogTitle",
                template: "{controller=Home}/{action=Index}/{blogTitle}");
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        };

2. Generate the URL

When putting the link into a page use markup like:

<a asp-route-blogTitle="@Model.BlogTitle" asp-controller="Blog" asp-action="Blog"
   class="a_s_btn myBTN">Read More</a>

3. Make sure the controller will receive and handle the new route

Instead of your current controller method that expects an id parameter, you need to take a blogTitle string:

public IActionResult Blog(string blogTitle)
{
   var blogModel = new BlogsListViewModel();
   blogModel.Blogs = repository.Blogs.OrderBy(p => p.BlogID).Where(p => p.BlogTitle == blogTitle);
   return View(blogModel);
}

Of course, the details of how you retrieve by title may change, depending on your variable. The important bit here is that the parameter name matches the new portion of the route provided in step 1.

Don't forget

Keep in mind that you will now need to handle special characters in your url. Urls can't contain some characters that your users could add to their blog title, such as ampersands or non-ascii characters. ( Characters allowed in a URL )

Jamie F
  • 23,189
  • 5
  • 61
  • 77
  • I tried this method (`Read More`), but what if the blog title matches another blog title? – Futuricon Nov 12 '19 at 07:40
  • Yes, that could be an issue. You could stop a user from saving a blog with a duplicate title, or append something when a duplicate is saved. For something similar, that is the approach I've taken: I save both title and UrlTitle. In UrlTitle I save the title with non-url characters removed, and can append a number if there is a duplicate. This problem is why it is more reliable to use Id as the link. – Jamie F Nov 12 '19 at 12:48