2

Is there anyway that I can exclude an ID field from the generated URL, but still be able to use the ID value? If you take a StackOverflow URL:

http://stackoverflow.com/questions/3409196/asp-net-mvc-routing-question

Can that URL be rendered without the Question ID?

1 Answer 1

1

You can display the question without an ID but question title has to be unique for every question.

Also you can still use the IDs for finding questions then redirect to another URL that only displays the question title. If that's how you wanna do it I can post an example.

Here's an example:

// this method finds a file from database using the id
//and passes the object with TempData
    public ActionResult InitialDetail(int id)
    {
        var question = questionRepository.GetFile(id);
        if (question==null)
            return View("NotFound");
        else
        {
            TempData["question"] = question;
            return Redirect("/questions/" + question.Name);
        }
    }


//this method uses model passed from other method and displays it
    public ActionResult Details(string questionName)
    {
        if (TempData["question"] == null)
        {
            return View("NotFound");
        }
        else
            return View("Details", TempData["question"]);
    }

You also have to define a route for this to work

routes.MapRoute("QuestionPage", //Files/id/fileName
            "questions/{questionName}",
            new { controller = "Questions", action = "Details" } );

Add this route just before the default route. It may mess things if you have routes for URL starting with http://domain.com/questions.

Note: This may not be the best solution. If your question titles are not unique, you can't put links with this structure in your page. First it has to look for question using ID.

Sign up to request clarification or add additional context in comments.

6 Comments

are redirects a bad practice? i could pass a view to another view then basically?
Unless I am misunderstanding, the redirect would be bad practice because you would not be able to copy and paste the URL to someone so they can see the same page (because the copy/paste won't have the TempData). I don't know how search engines would handle this either.
Yes that's true, otherwise URL has to contain smt that's speacial to that question. You can try using unique titles (if it's possible), that would be a lot easier.
if i redirected the title to a rewritten slug the copy and paste would still work? so if the title is "Routing and Url Structure", and i redirected to "routing-and-url-structure", copy and paste would work?
No. First you have be directed from URL with id. You already have to rewrite the title because URLs cannot contain spaces. If you look at the code the title is never checked, it just uses id for searching. You have to publish links like domain.com/questions/15 . I don't know how search engines handle this.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.