How to add page title in url in asp.net mvc?

Viewed 510

this is the Controller:

and this my route i want add title of news after id

for example:news/55/مایکروسافت،سرفیس دو

  [Route("News/{id}")]
        public ActionResult ShowNews(int id)
        {

            var news = pageRepository.GetPageById(id);
            if (news == null)
            {
                return HttpNotFound();
            }

            news.Visit += 1;
            pageRepository.UpdatePage(news);
            pageRepository.Save();

            return View(news);
        }

this is the page Repository:

   private MyCmsContext db;

    public PageRepository(MyCmsContext context)
    {
        this.db = context;
    }
    public IEnumerable<Page> GetAllPage()
    {
        return db.Pages;
    }

    public Page GetPageById(int pageId)
    {
        return db.Pages.Find(pageId);
    }

this is the Interface page Repository:

 IEnumerable<Page> GetAllPage();
    Page GetPageById(int pageId);

    bool InsertPage(Page page);
    bool UpdatePage(Page page);
    bool DeletePage(Page page);
    bool DeletePage(int pageId);
    void Save();
2 Answers

This is tested on ASP.Net Core 3.1: If you want to have a beautiful title (spaces replaced by dash for example), at first create an extension method like this:

 namespace BulkyBook.Utility
{
   public static class CleanURLMaker
    {
        public static string CleanURL(this string url)
        {
            // ToLower() on the string thenreplaces spaces with hyphens
            string cleanURL = url.ToLower().Replace(" ", "-");

            // cleanURL = System.Text.RegularExpressions.Regex.Replace(cleanURL , @"\s", "-");
            cleanURL = cleanURL.Replace(" ", "-");
            return cleanURL;
        }
    }
}

Then in your view.cshtml, the same place which you refer to/call your target, you have to pass your Title, something like this, but before sending the title, make it clean and beautiful by extension method you created above:

@using BulkyBook.Utility
 <a asp-area="Customer"  asp-controller="Home" asp-action="Details" asp-route-id="@item.ID" asp-route-Title="@item.Title.CleanURL()"> Details</a>

The upper code is equal to the below code:

<a  href="/HelloWorld/65/this-is-my.first-title"> Details</a>

and finally your action method will be like this:(attention, no need to pass Title as a parameter to your action method if you only want a clean URL):

 [Route("HelloWorld/{id}/{Title}")]
    public async Task<IActionResult> Details(int id)
    {
       Product product =await _unitOfWork.productRepository.GetByID(id);
       return View(product);
    }

Finally your link will be something like this : and no one see your area ,controller and action-method name

~/HelloWorld/23/this-is-my.first-title

If you wanna omit 'dot' and what ever you think, from url, just replace your favorite regex code in extension method.

> [Route("News/{id}/{title}")]
>         public ActionResult ShowNews(int id,string title)
>         {
> 
>             var news = pageRepository.GetPageById(id);
>             if (news == null)
>             {
>                 return HttpNotFound();
>             }
> 
>             news.Visit += 1;
>             pageRepository.UpdatePage(news);
>             pageRepository.Save();
> 
>             return View(news);
>         }
Related