区域和路线

时间:2009-10-23 10:13:56

标签: asp.net-mvc asp.net-mvc-areas

我到处都在使用区域,我想要的东西如下:

http://localhost/MyArea/MySection/MySubSection/Delete/20

通常我通过执行以下操作来访问事物:

http://localhost/MyArea/MySection/MySubSection/20

但如果我想删除,那么我必须说

http://localhost/MyArea/MySection/DeleteEntryFromMySubSection/20

有了路线,你怎么做? (顺便说一下,路线不现实,在我的系统中它们比这更简洁)

编辑:这与使用Areas,一个ASP.NET MVC 2 Preview 2功能特别相关。

1 个答案:

答案 0 :(得分:0)

这取决于你的路线和方式。控制器目前已经结构化。

以下是您可能想要使用的示例路线。

如果您希望能够调用以下路线进行删除:

http://localhost/MyArea/MySection/MySubSection/Delete/20

让我们假设您有一个名为“MyAreaController”的控制器,其操作为“删除”,为简单起见,我们假设部分和子部分只是字符串,例如:

public class MyAreaController : Controller
{
    public ActionResult Delete(string section, string subsection, long id)
    {

然后您可以通过以下方式创建路线(在Global.asax.cs中,或在您定义路线的任何地方):

var defaultParameters = new {controller = "Home", action = "Index", id = ""};            

routes.MapRoute("DeleteEntryFromMySubSection", // Route name - but you may want to change this if it's used for edit etc.
            "{controller}/{section}/{subsection}/{action}/{id}", // URL with parameters
            defaultParameters   // Parameter defaults
            );

注意:我通常会为所有可能的参数值定义枚举。然后params可以是适当的枚举类型,你仍然可以在你的路径中使用字符串。例如。你可以有一个“Section”枚举,它有一个“MySection”值。