【问题标题】:MVC Routing issue 404MVC 路由问题 404
【发布时间】:2015-02-24 13:28:08
【问题描述】:

这里有奇怪的路由问题。每次我尝试使用ViewForm 链接时,都会出现Resources Not Found 404 错误。当我使用 INDEXCREATE 链接时,我被路由到控制器中的正确功能。

我尝试了多种不同的链接配置,包括:

  • @Html.RouteLink("formView", New With { .formId = form.formId }) @Html.Action("View", New With {.formId = form.formId})

RouteConfig.vb

routes.MapRoute("form_list", "forms/list", New With {.controller = "Forms", .action = "Index"})
routes.MapRoute("form_view", "forms/view/{formId}", New With {.controller = "Forms", .action = "ViewForm"})
routes.MapRoute("form_create", "forms/{*formId}", New With {.controller = "Forms", .action = "Create"})

FormsController.vb

Function Index() As ActionResult
End Function

<ActionName("View")>
Function ViewForm(formId As Guid) As ActionResult
End Function

<HttpGet>
<ActionName("Create")>
Function Create_Get() As ActionResult
End Function

<HttpPost>
<ActionName("Create")>
Function Create_Post() As ActionResult
End Function

查看

INDEX>>> <a ID="lnkSideBarForms" href="@Url.RouteUrl("form_list")">Forms</a>
ViewForm>>> <a href="@Url.RouteUrl("form_view", New With {.action = "ViewForm", .formId = form.formId})" title="@form.formName">@form.formName</a>
CREATE>>> <a class="btn-list-action btn btn-small pull-right" href="@Url.RouteUrl("form_create")"><i class="icon-plus"></i>&nbsp;New Form</a>

【问题讨论】:

  • 我实际上对 VB 不是很熟悉,但问题可能是您没有传递有效的 Guid。尝试将 formId 更改为字符串,看看问题是否已解决。另一种方法是将 formId 标记为可选参数。

标签: asp.net-mvc razor model-view-controller routing http-status-code-404


【解决方案1】:

对 OP 的评论是问题所在。

我的控制器函数正在寻找 GUID,但我传递给他们的是 string

将我的控制器功能更改为以下功能解决了我的问题。

FormsController.vb

Function Index() As ActionResult
End Function

<ActionName("View")>
Function ViewForm(formId As String) As ActionResult
End Function

<HttpGet>
<ActionName("Create")>
Function Create_Get() As ActionResult
End Function

<HttpPost>
<ActionName("Create")>
Function Create_Post() As ActionResult
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-25
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    相关资源
    最近更新 更多