ASP.Net MVC中的假子控制器,还有更好的方法吗?

时间:2011-06-20 14:44:14

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

在使用ASP.Net MVC 3网络应用程序时,我发现需要有这样的路线.... http://mydomain.com/ParentPortal/Adult/Edit/4 其中Edit是命令,4是成人的ID。

我最终选择了以下路线......

routes.MapRoute("ParentPortal", "ParentPortal/{action}/{type}/{id}",
                New With {.controller = "ParentPortal", .action = "Index", .type = UrlParameter.Optional, .id = UrlParameter.Optional})

以下行动

<Authorize(Roles:="Parent")>
Public Function Adult(ByVal type As String, ByVal id As Integer?) As ActionResult
    Select Case type.ToLower
        Case "edit"
            Throw New NotImplementedException()
        Case "new"
            Throw New NotImplementedException()
        Case Else
            Throw New NotImplementedException()
    End Select

End Function

这是推荐的做法吗?

2 个答案:

答案 0 :(得分:1)

借调使用MVC区域。你现在所做的不是标准的ASP.NET MVC设计模式,也依赖于魔术字符串。

相反,创建一个名为“ParentPortal”的新区域。添加一个名为“Adult”的控制器,其中包含Edit和New actions。最后,使用MVC注册新区域。即:

  1. 创建'ParentPortal'区域 右键单击Web项目和 选择Add-&gt; Area ..

  2. 添加'AdultController'控制器 通过右键单击该区域到该区域 区域并选择添加 - >控制器

  3. 设置授权并添加编辑/新方法

    [Authorize(Roles="Parent")]
    public class Adult : Controller{
        public ActionResult Edit(int id){
           ..stuff..
           return View(model);
        }
    
        public ActionResult New(int id){
           ..stuff..
           return View(model);
        }
    }
    
  4. 4)验证Global.asax中的Application_Start是否包含:

    AreaRegistration.RegisterAllAreas();
    

答案 1 :(得分:0)

你看过MVC领域了吗?