Show a slug url without id in the url in asp.net mvc


Hello,

I am trying to have this kind of url

http://localhost:19901/announcements_search

instead of

http://localhost:19901/Home/Details/32/announcements_search

I have this kind of routing

 routes.MapRoute(
               name: "Slug",
               url: "{slug}",
               defaults: new { controller = "Home", action = "Details", slug = UrlParameter.Optional }
           );

but it doesn't work

It gives 

http://localhost:19901/?id=16

In my controller I have

public ActionResult Details(int? id, string slug)
        {


            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            SiteContent sitecontent = db.sitecontent.Find(id);

            if (sitecontent == null)
            {
                return HttpNotFound();
            }
            if (string.IsNullOrWhiteSpace(slug))
            {

                slug= db.sitecontent.First(p => p.ContentID == id).Contentslug;



                return RedirectToAction("/Details/" + id + "/" + slug);
            }



            return View();
        }

I know that is wrong way because I pass the id. I don't know how to change it so to success it. 

Any idea?

thank you


Asked by:- mspace
0
: 5362 At:- 3/19/2018 8:00:00 AM
mvc routing slug url without id







3 Answers
profileImage Answered by:- pika

I would not recommend you to create Slug url without Id in it, because of two reasons.

  1. You cannot create same slug for any other post.
  2. You would need to match Data using slug url in instead of ID, which will slow down your search.

If you still want to use it, you need code like below(If you just need slug in url no ID)

@Url.Action("Details", "Home", new { slug = item.Slug })

In your controller

public ActionResult Details(string slug)
{
    Post post = db.Posts.Where(x=> x.urlslug == slug).FirstOrDefault();
     //other code
}

In your RouteConfig

 routes.MapRoute(
                name: "PostWithSlug",
                url: "{slug}",
                defaults: new { controller = "Home", action = "Details" }
                );

Note: This Should be placed before Default routing

Now you can call use URL like "http://localhost:57855/Your-slug-url"

In case you want to attach Id with Slug in your url follow Vikas's answer on this question https://qawithexperts.com/questions/37/how-can-i-simplify-url-in-net-mvc

This is the correct way.

Also you cannot hide Id from the URL if you want to fetch data using Id, you can either show ID & slug or only slug.

3
At:- 3/19/2018 8:37:14 AM Updated at:- 3/20/2018 12:26:33 PM


profileImage Answered by:- vikas_jk

Here is the working demo of the code which is explained by @pika

slug-url-without-id-in-mvc-min.gif

You can check in the .gif, my RouteConfig,Details ActionMethod in controller and View results.

1
At:- 3/19/2018 11:27:47 AM
I dont know what I doing wrong. I am going to see it 0
By : mspace - at :- 3/19/2018 11:46:52 AM
its ok now! I had to change some code to my partial View! thank you all! 1
By : mspace - at :- 3/20/2018 8:47:15 AM


profileImage Answered by:- mspace

Thank you for your reply,

now I get 

HTTP Error 400.0 - Bad Request

and my url becomes

http://localhost:19901/Home/Details

0
At:- 3/19/2018 8:52:19 AM
Why the above answer doesn't work for you??? Something wrong with your tried the above answer it works 0
By : vikas_jk - at :- 3/19/2018 11:19:16 AM
My slug always returns null. I dont know why. I am searching it 0
By : mspace - at :- 3/19/2018 11:34:18 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use