开发者

object oriented url scheme in asp.net mvc 2

I'd like to modify the standard ASP.NET MVC URL routes

http://example.com/Controller/Action[/Id]

to something like:

http://example.com/Controller/Id/Action

Modifying the route scheme is trivial but I'm getting into trouble when handling default values. I would need these URLs

http://example.com/Controller/ -> Maps to Index() -> List of items
http://example.com/Controller/Create
http://example.com/Controller/Id[/Details] -> Maps to Details(id)
http://example.com/Controller/Id/Edi开发者_如何转开发t -> Maps to Edit(id)
http://example.com/Controller/Id/Delete -> Maps to Delete(id)

Yikes! This means quite a few routes instead of the nice default one. Or can I use URL parameters with default values in the middle of the URL?

The next step is to use a friendly (yet unique!) object reference (eg customer name. Let's assume it is unique) instead of a DB record identity (id). Such as:

http://example.com/Controller/Name[/Details] -> Maps to Details(id)

So far so good but I'm not sure it's a good idea to use this for the Edit (and possibly delete) page: The edit form lets user modify the object name, leading to possible mismatches in url. Should I stick to the id in Edit URLs?

Did anyone experience with such URLs? Did you push the idea to hierarchical systems? Is this all a good idea or will I dive into more trouble than benefits?

TIA for your thoughts.


You can do this only if the ID parameter is always present in the URL. For obvious reasons optional parameter can only be at the end of the url or it is impossible to disambiguate them.


Try this:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{id}/{action}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Controller goes to home by default and will fire up the Index action of not said other wise. Id is as well optional. All I actually changed was the order of parameters.

As for the res of the question. If you're users are logged and they edit their profile for example you could get the Id of what to edit from somewhere else - a cookie for example. I do this in my user management. URL /User/Edit is unique for the user that's logged in.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜