Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

c# - URL Rewriting in .Net MVC

I'm wondering what is the best way to handle URL in MVC. For example, in my application I have a PageController can link to /website/Page/Index/3 or /website/Page/home. The menu is built dynamically with Html.ActionLink() and T4MVC based on the incoming urls so I don't have anything hardcoded.

Now what I want to do is to point my url and links to something more SEO friendly like, for example, /website/our-company/ and it can also have children like /website/our-company/location/ or /website/our-company/employees/. You get the idea.

All my Pages are saved to the BD and I have FriendlyUrl and parentId properties in my object.

What's is the best way of doing it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I took a look at http://www.asp.net/learn/mvc/tutorial-23-cs.aspx and I got it working. More simple than I thought...

My routes :

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute("Pages3", "{url1}/{url2}/{url3}", MVC.Page.RedirectTo(), new { url1 = "", url2 = "", url3 = "" });
    routes.MapRoute("Pages2", "{url1}/{url2}", MVC.Page.RedirectTo(), new { url1 = "", url2 = "", url3 = "" });
    routes.MapRoute("Pages1", "{url1}", MVC.Page.RedirectTo(), new { url1 = "", url2 = "", url3 = "" });
}

And now my controller :

public virtual ActionResult RedirectTo(string url1, string url2, string url3)
{
    if (string.IsNullOrEmpty(url1)) return Home();

    var pageModel = new PageModel();
    pageModel.CurrentPage = _pageRepo.GetByUrl(url1, url2, url3);
    BuildMenusAndBreadCrumb(pageModel);
    ViewData.Model = pageModel;

    return View(Views.Index);
}

And here's how I render a link (my menu exemple) :

<div class="header_menu_content">
<ul id="main_menu_header">
    <% foreach(var item in Model) {%>
        <% if(item.Children != null){ %>
            <li><%= Html.ActionLink(item.Title, MVC.Page.RedirectTo(item.Url, "", ""))%>
            <ul>
            <% foreach (var child in item.Children){ %>
                <li><%= Html.ActionLink(child.Title, MVC.Page.RedirectTo(item.Url, child.Url, "")) %></li>
            <% }%>
            </ul>
            </li>
        <% } else { %>
            <li class="nochild"><%= Html.ActionLink(item.Title, MVC.Page.RedirectTo(item.Url, "", "")) %></li>
        <% } %>
    <%} %>
</ul>

Works perfectly for my needs! If you have any question or comments don't be shy! I'm not sure it's the best way to do it but I'm happy with it!

Note that the route order is important and also, if you don't put the default value and are in a page where url2 = something (site/section/page) then all your link will point to site/newsection?url2=page took me a while to figure out why that url2 param was there but now it's all right!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...