简体   繁体   English

Asp.net MVC2 URL结构-最佳做法

[英]Asp.net MVC2 URL structure - best practices

What are the best practices for setting up projects with multiple interfaces in asp.net MVC2? 在asp.net MVC2中设置具有多个接口的项目的最佳实践是什么? I'm starting a project where we need a typical arrangement like: 我正在开始一个项目,我们需要一个典型的安排,例如:

example.com/ - end-user interface example.com/-最终用户界面

example.com/Admin/ - site management example.com/Admin/-网站管理

The site will be involved enough that simply having all the user logic in HomeController and all the admin logic in AdminController isn't really an option. 该网站将足够参与,只需在HomeController中拥有所有用户逻辑,并且AdminController中的所有管理逻辑都不是真正的选项。 I've read about using multiple Route definitions, but the following doesn't seem to work properly: 我已经阅读了有关使用多个Route定义的信息,但以下内容似乎无法正常工作:

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

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

It causes all the links on the homepage to point to example.com/Admin/local-part instead of example.com/local-part. 这会使主页上的所有链接都指向example.com/Admin/local-part而不是example.com/local-part。

I'd like to avoid specifying a different MapRoute for each Admin controller (omitting {controller} from the mapping each time). 我想避免为每个管理控制器指定不同的MapRoute(每次都从映射中省略{controller})。

I've also read about setting up different Areas within a project, but that seems like it would be too involved for the scope of this project. 我还读过有关在项目中设置不同区域的信息,但对于该项目的范围来说似乎太过复杂了。

Finally, I've also read that you can put constraints on MapRoutes, but the documentation on that part seems confusing to me. 最后,我还读到可以在MapRoutes上设置约束,但是那部分的文档对我来说似乎很混乱。 Is there something obvious I'm missing? 有什么明显的我想念的东西吗? Failing that, are there any best practices for asp.net structure that I should keep in mind? 如果没有,我应该记住关于asp.net结构的最佳实践吗?

It sounds like Area's is ready made for what you want to do. 听起来像Area已经准备好了你想要做的事情。 Setting up an area isn't really all that involved, basically you just have to register it. 设置一个区域实际上并不是所有涉及的,基本上你只需要注册它。 By default the area routing will match the default in the global.asax, the exception being the extra "\\area" slug in the url. 默认情况下,区域路由将与global.asax中的默认路由匹配,但URL中多余的“ \\ area”子段除外。 I'm fairly certain it only took me a few minutes when I set this up on a project a few months ago. 我相当肯定,几个月前,当我在一个项目上设置它时,我只花了几分钟。

If your Admin controller is complicated enough to exceed the scope of a single controller then that indicates that an area may be warranted. 如果您的管理控制器复杂到足以超出单个控制器的范围,那么表明可能需要保证区域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM