【问题标题】:How can I make this Route (ASP.Net MVC2)?我怎样才能制作这条路线(ASP.Net MVC2)?
【发布时间】:2010-04-11 17:08:02
【问题描述】:

我是 asp.net mvc 的初学者,我对路由有一些疑问。

我正在开发一个系统来管理文档,我需要创建一个这样的 URL:

        routes.MapRoute("Documentos",
            "{controller}/{documentType}/{documento}/{action}/{id}",
            new
            {
                controller = "Home",
                documentType = "",
                documento = "",
                action = "Index",
                id = UrlParameter.Optional
            });

应用程序使用如下 URL:

"Document/Administrative/Contract" - (索引操作默认列出“合同”类型的文档) “文档/管理/合同/新建” - (控制器中的新操作) “Document/Administrative/Contract/10” - (控制器中的详细操作) “Document/Administrative/Contract/Edit/10” - (在控制器中编辑操作)

Document 将是一个 Controller,Administrative 将只是 url 中的描述,以标识“合同”的文档是Administrative...

所以,我的疑问是关于我的控制器和动作,控制器方法的签名应该如何?我需要创建一个名为 Documents 的区域来更轻松地执行此操作吗?

PS:对不起我的英语!

非常感谢, 干杯!

费利佩

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-2 routing


    【解决方案1】:

    只有 1 个控制器文档。

    在这个控制器中有actions索引 ,新建和编辑。

    所有动作都需要有参数 documentType 和 documento 或者(更好)你有一个 Viewmodel 作为具有属性 documentType 和 documento 的参数。

    将此 Viewmodel 传递给视图。在视图中使用 Html.ActionLink 生成具有 documentType 和 documento 集的链接。

    【讨论】:

      【解决方案2】:
       routes.MapRoute("Documentos", 
                  "Document/{documentType}/{documento}/{action}/{id}", 
                  new 
                  { 
                      controller = "Document", 
                      documentType = "", 
                      documento = "", 
                      action = "Index", 
                      id = UrlParameter.Optional 
                  }); 
      

      当您希望“文档/管理/合同”的 URL 默认为列表时,您必须像这样进行硬编码路由:

       routes.MapRoute("Documentos", 
                  "Document/Administrative/Contract", 
                  new 
                  { 
                      controller = "Document", 
                      documentType = "Administrative", 
                      documento = "Contract", 
                      action = "List"
                  }); 
      

      您的操作应该包含与您在路由中使用的参数名称相同的名称。或者结合模型类中的参数。

      public ActionResult(string documentType, string documento, int id)
      

      public ActionResult(Document doc)
      

      如果您使用模型类文档。

      public class Document
      string documentType;
      string documento;
      int id;
      

      HTH

      【讨论】:

        猜你喜欢
        • 2015-03-13
        • 2020-04-12
        • 1970-01-01
        • 2012-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-23
        • 1970-01-01
        相关资源
        最近更新 更多