Hamid Reza
Hamid Reza

Reputation: 2973

How to make url's friendly with asp mvc routing?

I am using asp mvc3.I want to learn how to make my Urls friendly. this is my repository layer:

public Section GetBySectionId(int sectionId)
{
    return context.Sections.Include("Groups").Include("Partners").Where(s => s.SectionId == sectionId).FirstOrDefault();
}

And this is the application layer:

public Section GetBySectionId(int sectionId)
{
    return sectionRepo.GetBySectionId(sectionId);
}

And this is the controller:

public ActionResult Details(int id)
{
    var section = Mapper.Map<SectionViewModel>(sectionApp.GetBySectionId(id));
    return View(section);
}

Now for example if I go to a section's details with id=3 the browser url would change to ~/Section/Details/3 but I want it to be ~/Section in persian/Details in persian/My section name.How can I do this.How to work with asp mvc routing?

Upvotes: 0

Views: 315

Answers (1)

Rajpurohit
Rajpurohit

Reputation: 1991

You should change in your Routing like this

routes.MapRoute(..., "{controller}/{action}/{id}", ...);

... and change it to something like this:

routes.MapRoute(..., "{controller}/{action}/{name}", ...);

Then have your action take the name instead of the ID:

Html.ActionLink(item.Name, "Details", new {item.Name})

Upvotes: 2

Related Questions