【发布时间】:2016-10-05 14:29:07
【问题描述】:
我有一个 Post Controller 应该处理这样的路由:
mywebsite.com/post/somthing
mywebsite.com/post/post1
mywebsite.com/post/anotherpost
...
当我在网上搜索我的答案时,我发现如何像这样注册我的地图路线:
routes.MapRoute(
name: "post",
template: "post/{*postname}",
defaults: new { controller = "Post", action = "ReadPost" });
但我不知道当我为我的路由创建控制器和操作方法时,如何将 {*article} 作为我的方法的输入,以便该方法知道要返回视图的模型。
我的控制器也应该处理以下路由:
mywebsite.com/post/anotherpost/comments
mywebsite.com/post/anotherpost/edit
mywebsite.com/post/anotherpost/author
当路由以什么都没有结束时,它应该被重定向到ReadPost(string postName)
当路由以 cmets 结尾时,它应该被重定向到 ReadComment(string postName)
当路径以编辑结束时,它应该被重定向到Edit(string postName)
当路由以作者结尾时,它应该被重定向到ReadAuthor(string postName)
我应该如何编写控制器和路由映射,以便正确的路由连接到正确的操作并为方法提供“帖子名称”作为输入?
【问题讨论】:
-
您写道,“...如何将 {*article} 作为输入...”
article是什么? -
is {antherpost} 在您的问题中是字符串 postName?
-
@ShaunLuttin 抱歉,我修正了一个错字
-
@Liran 是的,这是一个字符串名称。
标签: c# asp.net-mvc asp.net-core