ASP MVC 5路线映射

时间:2018-04-14 19:03:02

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

Insides my views文件夹我有一个' User'文件夹,这不包含相应的视图,但包含将包含视图的子文件夹。

示例:

User
 Create
   create.cshtml
 Edit
   edit.cshtml

然后我有一个控制器,目前只有一个用于创建的操作:

public ActionResult Create() {
    return View("Create/Create");
}

因此,为了执行该操作,我转到user/create,这会使视图恢复正常。

问题来自于我尝试使用<a>Url.RouteUrl标记中链接到该视图时。它出错并说:

  

名为&#39; user / create&#39;的路线在路线集合中找不到

但是对我来说,它匹配路由配置中的默认路由:controller/action

这是我的RouteConfig:

public static void RegisterRoutes(RouteCollection routes) {
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

  // Turn on attribute routing in the controllers
  routes.MapMvcAttributeRoutes();

  routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new {
      controller = "Home", action = "Index", id = UrlParameter.Optional
    }
  );
}

那么为什么我需要为它定义自定义路线?

1 个答案:

答案 0 :(得分:1)

您无需为其定义自定义路由,但您需要遵循文件夹的约定。将create.cshtml和edit.cshtml视图直接放在User文件夹中,并删除Create和Edit子文件夹,不需要它们。

现在,在控制器中,您只需使用:

Option Explicit

Public Sub UpdateBtn()

    Const WS1_NAME = "GB_Data"
    Const WS2_NAME = "Lists"

    Const START_ROW1 = 2    'in GB_Data
    Const START_ROW2 = 2    'in Lists

    Const COL1 = "D"        'in GB_Data
    Const COL2 = "E"        'in Lists
    Const COL3 = "F"        'in Lists

    Dim ws1 As Worksheet:   Set ws1 = ThisWorkbook.Worksheets(WS1_NAME)
    Dim ws2 As Worksheet:   Set ws2 = ThisWorkbook.Worksheets(WS2_NAME)
    Dim lr1 As Long:        lr1 = ws1.Cells(ws1.Rows.Count, COL1).End(xlUp).Row
    Dim lr2 As Long:        lr2 = ws2.Cells(ws2.Rows.Count, COL2).End(xlUp).Row
    Dim arr1 As Variant:    arr1 = ws1.Range(COL1 & START_ROW1 & ":" & COL1 & lr1).Formula
    Dim arr2 As Variant:    arr2 = ws2.Range(COL2 & START_ROW2 & ":" & COL3 & lr2).Formula

    Dim r1 As Long, r2 As Long

    For r1 = 1 To UBound(arr1)
        For r2 = 1 To UBound(arr2)
            If arr1(r1, 1) = arr2(r2, 1) Then arr1(r1, 1) = arr2(r2, 2)
        Next r2
    Next r1
    ws1.Range(COL1 & START_ROW1 & ":" & COL1 & lr1).Formula = arr1
End Sub

它知道要查找什么以及在哪里找到它,因此它将成功选择您的User / create.cshtml视图。但是,我们通常会将模型传递给我们的视图。在像Create这样的视图中,模型用于构建所有控件(文本框,标签等):

public ActionResult Create() {
    return View();
}