1

我对 MVC 完全陌生,如果我使用了错误的术语,请原谅。我正在构建一个使用以下格式的控制器来显示一个项目,以及该项目中的一个步骤。

注意:我使用的是 MVC5,它使用了新引入的路由属性。

'/project/1/step/2
<Route("{ProjectID}/Step/{StepNumber:int}")>
Function ProjStep(ProjectID As String, StepNumber As Integer) As String
   Return String.Format("Project {0} Step {1}", ProjectID, StepNumber)
End Function

以上按预期工作。但我也想处理用户只指定项目而不指定步骤的情况。

'/Project/1
<Route("{ProjectID}")>
Sub Projuate(ProjectID As String)
   'Automatically start the user at step 555  
   'How do I send the user to the URL /Project/ProjectID/Step/555  
End Sub
4

2 回答 2

2

Apologies if syntax is a bit off - I do my MVC in C#.

You can either call the step Action directly or redirect to it.

Former (this will leave URL as /Project/1):

Function Projuate(projectID As String)
  Return ProjStep(projectID,555)
End Sub

Latter (will end up on /Project/1/Step/555):

Function Projuate(projectID As String)
  Return Redirect(Url.Action("ProjStep", new{ ProjectID = projectID, StepNumber=555})
End Sub

I don't know whether T4MVC works with VB but I would check that out - as it means you can get rid of those magic strings and get some nice extensions for creating URLs.

EDIT NOTE: Changed Sub to Function.

于 2013-12-10T14:58:20.597 回答
0

只需将默认值添加到路由可能是最简单的方法:

<Route("{ProjectID}/Step/{StepNumber:int=555}")>
Function ProjStep(ProjectID As String, StepNumber As Integer) As String
    Return String.Format("Project {0} Step {1}", ProjectID, StepNumber)
End Function

ps 我希望它出现在 int 之后 - 据我所知,它可能是 StepNumber=5:int。

于 2014-01-14T15:20:19.610 回答