Custom Routing Rules (e.g. www.app.com/project/35/search/89/edit/89)

北慕城南 提交于 2019-12-09 22:05:47

问题


I would like to create routing rules like the following:

www.app.com/project/35/search/89/edit/48 ---> action is edit in the project controller

The passed variables should be project# (35), search# (89), and edit#(48)

Can someone help me structure a routes.MapRout() for this.

I want to use:

routes.MapRoute(
            "Default",
            "{controller}/{projectid}/search/{searchid}/{action}/{actionid}",
            new { controller = "Home", action = "Edit", projectid = "", actionid = "" }
        );

But from previous experience, this type of MapRoute will fail... I've only gotten something in the following format to work:

{controller}/{action}/{variable}

Can anyone give me any advice on this? Thanks.


回答1:


Honestly, it sounds like you need to make you URL's look like this:

www.app.com/project/35?search=89&edit=48&flag=63 

It would make your like much simpler.




回答2:


Grouping Controllers with ASP.NET MVC

A question that often comes up is how do you group controllers when building a large application with ASP.NET MVC. Often, the question is phrased as whether or not ASP.NET MVC supports “Areas”, a feature of Monorail. According to the Monorail documentation,

MonoRail supports the concept of areas, which are logical groups of controllers. All controllers belong to an area. The default area is an empty (unnamed) one

While there’s no out of the box support for this in ASP.NET MVC, the extensibility model allows building something pretty close.

Registering Routes

The first thing we do is call two new extension methods I wrote to register routes for the areas. This call is made in the RegisterRoutes method in Global.asax.cs.

routes.MapAreas("{controller}/{action}/{id}", "AreasDemo", new[]{ "Blogs", "Forums" });

routes.MapRootArea("{controller}/{action}/{id}", "AreasDemo", new { controller = "Home", action = "Index", id = "" });The first argument to the MapAreas method is the Routing URL pattern you know and love. We will prepend an area to that URL. The second argument is a root namespace. By convention, we will append “.Areas.AreaName.Controllers” to the provided root namespace and use that as the namespace in which we lookup controller types.

http://haacked.com/archive/2008/11/04/areas-in-aspnetmvc.aspx




回答3:


Why did this format fail for you? What do you mean by "failed"? If it just didn't work, try placing your new MapRoute above the default commands - the Routes collection is working in a FIFO manner.

@Robert Harvey - Take a look at Basecamp for example - they are using a very similar approach in their URI's. I think it's a much cleaner and better approach than what you're suggesting. It's not even "Simpler" once you get the hang of routing.



来源:https://stackoverflow.com/questions/1080415/custom-routing-rules-e-g-www-app-com-project-35-search-89-edit-89

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!