开发者

asp.net mvc routing with optional starting parameter

开发者 https://www.devze.com 2023-01-15 07:56 出处:网络
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.

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-article

edit/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? S开发者_运维技巧hould I just start with a custom routing class for maximum flexibility?


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:

[language]/[category]/[subcategory]/[itemtitle]/[itemid]

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!

0

精彩评论

暂无评论...
验证码 换一张
取 消