C# .Net MVC 中的 URL 重写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2375256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
URL Rewriting in .Net MVC
提问by VinnyG
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.
我想知道在 MVC 中处理 URL 的最佳方法是什么。例如,在我的应用程序中,我有一个PageController
可以链接到/website/Page/Index/3
或 的链接/website/Page/home
。菜单是使用 Html.ActionLink() 和 T4MVC 基于传入的 url 动态构建的,所以我没有任何硬编码。
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.
现在我想要做的是将我的 url 和链接指向更 SEO 友好的东西,例如,/website/our-company/
它也可以有像/website/our-company/location/
或/website/our-company/employees/
. 你明白了。
All my Pages are saved to the BD and I have FriendlyUrl
and parentId
properties in my object.
我所有的页面都保存到 BD 中,FriendlyUrl
并且parentId
我的对象中有和属性。
What's is the best way of doing it?
最好的方法是什么?
采纳答案by VinnyG
I took a look at http://www.asp.net/learn/mvc/tutorial-23-cs.aspxand I got it working. More simple than I thought...
我看了一下http://www.asp.net/learn/mvc/tutorial-23-cs.aspx并且我让它工作了。比我想象的要简单...
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!
请注意,路由顺序很重要,而且,如果您不设置默认值并且位于 url2 = something (site/section/page) 的页面中,那么您的所有链接都将指向 site/newsection?url2=page take我花了一些时间弄清楚为什么那个 url2 参数在那里,但现在一切都好了!