tags:

views:

237

answers:

3

Can anyone help me? I'm doing some tests in ASP.NET MVC.

I want to implement the nice URL as stackoverflow.com routing system.

For example: stackoverflow.com/questions/1074/asp-mvc domain.com/id/title

Here is my code:

  1. in the Global.asax

    routes.MapRoute(null, "posts/{PostId}/{Title}", new { controller = "Posts", action = "Details", post = (string)null }, new { PostId = @"\d+", Title = @"([A-Za-z0-9-]+)" } );

  2. in the View:

    <%= Html.ActionLink(Model.Title, "Details", new { Model.PostID, Model.Title})%>

With those codes, I am geting the url : http://localhost:53171/posts/Details?PostID=5&amp;Title=Test-title

Can one one advise me?

Thank you very much.

+2  A: 

Not sure what all of the stackoverflow url means, but if you wanted a clean url like:

http://stackoverflow.com/Questions/132/thetitle

In your Global.asax:

    routes.MapRoute("PostController",
       "Questions/{post_id}/{post_title}",
       new { controller = "Post", action = "Index" },
       new { post_id = @"\d+", post_title = @"([A-Za-z0-9-]+)" }
    );

In your controller retrieve the values:

    public ActionResult Index(Int32 post_Id, String post_title)
    {
        return View();
    }

To produce the correct url use Html.RouteLink:

<%= Html.RouteLink("The Title", "PostController", new { post_id = 132, post_title = "thetitle" })%>
Nick Clarke
A: 

You are on the right path but you just need to match the name of your Controller ActionMethod parameters to the object route values in your Html.ActionLink.

public ActionResult Index(int PostId, string Title)
{
    ...
    return View();
}

<%= Html.ActionLink(Model.Title, "Details", 
    new { PostId = Model.PostID, Title = Model.Title}) %>
David Liddle
+1  A: 

I think you need to put some default values into your route for the post title... and make sure they map through. You don't seem to have default values for 'postId' and 'title' and yet you have one for a non-existant 'post' route value.

routes.MapRoute(
    "PostDetails",
    "posts/{postId}/{title}",
    new { controller = "Posts", action = "Details", postId = 0, title = "" },
    new { PostId = @"\d+", Title = @"([A-Za-z0-9-]+)" }
);

Posts controller

public ActionResult Details(int postId, string title)
{
    //whatever
}

Then in your view

<%= Html.ActionLink(Model.Title, "Details", new { @postId = Model.PostID, @title = Model.Title }) %>

Or

<%= Html.ActionLink(Model.Title, "Details", "Posts", new { @postId = Model.PostID, @title = Model.Title }, null) %>

I would also suggest creating a TitleSlug property on your posts model.

E.g. (code taken from here)

public partial class Post
{
    public string TitleSlug
    {
        get
        {
            string str = Title.ToLower();

            str = Regex.Replace(str, @"[^a-z0-9\s-]", ""); // invalid chars       
            str = Regex.Replace(str, @"\s+", " ").Trim(); // convert multiple spaces into one space
            str = str.Substring(0, str.Length <= 45 ? str.Length : 45).Trim(); // cut and trim it
            str = Regex.Replace(str, @"\s", "-"); // hyphens

            return str;
        }
    }
}

HTHs
Charles

Charlino