URL重写在.net MVC [英] URL Rewriting in .Net MVC

查看:166
本文介绍了URL重写在.net MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是处理URL在MVC的最佳途径。例如,在我的应用我有一个的PageController 可以链接到 /网站/页/索引/ 3 /网站/页/家居。菜单是根据传入的URL,所以我没有什么难$ C $光盘Html.ActionLink()和T4MVC动态构建。

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.

我的所有页面都保存到BD和我有 FriendlyUrl parentId的在我的对象的属性。

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?

推荐答案

好吧,我看了看的 http://www.asp.net/learn/mvc/tutorial-23-cs.aspx ,我得到了它的工作。更简单,比我因子评分...

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

我的路线:

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!

请注意,该航线顺序是很重要而且,如果你不把默认值,并在一个页面,网址2 =东西(网站/节/页),则所有的youre链接将指向网站/ newsection?URL2 =页我花了一段时间才能找出为什么URL2参数是有,但现在它的所有权利!

Note that the route order is important and also, if you dont put the default value and are in a page where url2 = something (site/section/page) then all youre 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!

这篇关于URL重写在.net MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆