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
165 views
in Technique[技术] by (71.8m points)

ASP.NET Routing Question

Why is this:

http://MySite.com/Project/24/Search/32/Edit/49

preferred over this?

http://MySite.com/Project/24?Search=32&Edit=49
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

I'm not sure where your premise is coming from? It looks like an artificial example, which makes it hard to comment on.

A better comparison would be something like:

http://MySite.com/Project/24/Members/Edit

As opposed to:

http://MySite.com/Projects.aspx?id=24&section=Members&action=Edit

Where, among other things, the hierarchy of entities is immediately obvious from the first example (ie, a Project contains Members). It also suggests that you can use other URLs that contain similar structures to the first (ie, /Projects/24 and /Projects/24/Members), so in that sense it's more concise.

If it comes down to actions that have a variable number of parameters, such as searching, then it's totally fine to use URL parameters as this will give you more flexibility, eg:

http://MySite.com/Projects/Search?name=KillerApp&type=NET

You could construct a URL using the first style, but you don't really gain anything, and managing the route could add unnecessary overhead:

http://MySite.com/Projects/Search/name/KillerApp/type/NET

I would argue that this (or any similar construction, eg if you removed the param names) suffers from an artificial hierarchy - the action in this case is really Search, and everything else is just a parameter of the Search, so it's in the same hierarchy, not some "sub" hierarchy.


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

...