带有可选启动参数的 asp.net mvc 路由
我正在启动将现有 ASP.NET Web Forms CMS 移植到 ASP.NET MVC,并希望从一开始就获得正确的路由。
注意:具有完全相同的 URL 结构并不重要。
我认为 这个答案 接近我正在寻找的但想要的任何人都应该有一些额外的输入。
当前的 URL 结构如下:
?Content=新闻/CurrentNews/This_is_a_news_article
?Content=公司/关于我们/概述
等等
我想添加一个可选的语言参数并在 MVC 中保留类似的结构。所以像这样:
新闻/CurrentNews/这是一篇新闻文章
zh/News/CurrentNews/This-is-a-news-article编辑/新闻/CurrentNews/这是一篇新闻文章
编辑/en/News/CurrentNews/This-is-a-news-article
还是我反过来更好?
新闻/CurrentNews/这是新闻文章/编辑
zh/News/CurrentNews/This-is-a-news-article/edit
我认为这种方式(结束操作)需要为我读过的其他问题中的每个场景提供一条路线。
另一点是现有的 URL 是像 SEO 和面包屑生成那样完成的。即 URL 显示当前导航路径。
我可以只在 URL 中显示当前页面,并分别从数据库构建面包屑。
喜欢:
en/这是一篇新闻文章
带面包屑的
新闻文章首页 >新闻动态>>当前新闻>这是一篇新闻文章
总体思路和解决方案?我应该从自定义路由类开始以获得最大的灵活性吗?
I'm starting a port of an existing ASP.NET Web Forms CMS to ASP.NET MVC and want to get the routing right from the start.
Note: it isn't important to have the exact same URL structure.
I think this answer is close to what I'm looking for but would like some additional input should anyone have it.
The current URL struction is like:
?Content=News/CurrentNews/This_is_a_news_article
?Content=Corporate/About_Us/Overview
etc etc
I would like to add an optional language paramater and keep a similar structure in MVC. So something like:
News/CurrentNews/This-is-a-news-article
en/News/CurrentNews/This-is-a-news-articleedit/News/CurrentNews/This-is-a-news-article
edit/en/News/CurrentNews/This-is-a-news-article
or am I better with the reverse?
News/CurrentNews/This-is-a-news-article/edit
en/News/CurrentNews/This-is-a-news-article/edit
I think this way (with action on end) requries a route for every scenario from other questions I've read.
The other point is that the existing URLS are done like that for SEO and bread crumb generation. ie the URL shows the current navigation path.
I could just show the current page in the URL and build the bread crumbs from database separatly.
Like:
en/This-is-a-news-article
with crumb
Home > News > Current News > This is a news article
Overall thoughts and solutions? Should I just start with a custom routing class for maximum flexibility?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须确保某些路由规则不会意外重叠。例如,假设您将语言作为第一个标签,请确保不存在名称为“en”的新闻类别。我知道这是一个愚蠢的例子,但我想你已经明白了。确保将最不准确的路由规则放在最后。
就我个人而言,我会这样构造它:
为什么你可能会问 [itemId]?如果您有一个较大的网站,则可能会出现 2 篇具有完全相同名称的新闻文章。您的代码可能“恰好”编写为仅显示返回的第一个新闻站点,但也可能不是。 ID 始终是唯一的,就 SEO 而言,添加 ID 并不重要(至少根据我的经验)。
您可以添加一条省略 [语言] 并提供默认值的路线。
另外,Scott Guthrie 写了一篇很好的 博文 不久前关于此的内容绝对值得一读,我认为它对于 MVC 3 仍然有效。它可能会给您一些想法!
You have to make sure that there will be no accidental overlap of certain routing rules. For example say you are putting the language as the first tag, make sure there will be no news-categories that have 'en' for a name. I know this is a stupid example but I think you get the picture. Make sure you put the least accurate routing rules last.
Personally I would structure it like this:
Why the [itemId] you may ask? If you have a larger website, it might happen that you have 2 news articles with the exact same name. Your code may "just happen to be" written to only show the first newsitem returned, but it just as well may not be. An ID is always unique and in SEO terms, adding the ID, really does not matter (in my experience at least).
You can add a route where [language] is left out and a default is provided instead.
Also, Scott Guthrie has written a nice blogpost on this a while ago that's definitely worth a read, I think it's still valid for MVC 3. It may just give you some ideas!