I have an asp.net 4.6.1, mvc 5 webapp with attribute routing. The code is

[Route("Cars/{id:int?}/{title}")]
public ActionResult Details(int? id, string title)
{
    if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
    Cars car = db.Cars.Find(id);
    if(title == null || title.ToLower()!= ParseTitleToUrlVersion(car.Title.ToLower()))
        {
              return Redirect("/cars/" + id + "/" + ParseTitleToUrlVersion(questions.Title));
        }
    If(cars == null)
        {
              // return not found;
        }
    Return View(car);
}

The details view successfully renders the details of the car with id = 3 for these url's

localhost:44300/cars/3/my-correct-car-title
localhost:44300/cars/3/any-random-string

but when i try to browse this url which doesn't specify the title

localhost:44300/cars/3

it says that it could not find the resource. I want it also to redirect to the url with correct title. The code also says if title is null, redirect it to the first url. For example, these two url's give the same page

https://stackoverflow.com/questions/36421657/npm-express-module-not-installing

and

https://stackoverflow.com/questions/36421657

I also want that when the second url is browsed, it redirects to the correct url. Kindly help in finding where is the error in my code.

Related posts

Recent Viewed