如何根据以下模式映射路线?

发布于 2024-11-07 20:25:14 字数 776 浏览 0 评论 0原文

你如何映射这个网址
/Topic/topicName/action?topicId=someInt

其中 Topic 是控制器名称(这是 const - 它始终读取“Topic”),
topicName 被忽略
action 是操作名称,
someInt 是“action”的 topicId 参数。

以下是几个示例:
/Topic/c-sharp/AddQuestion?topicId=1

调用:
控制器:主题
操作:添加问题
topicId =1

这是 TopicConroller 中操作的特征

public ActionResult AddQuestion(int topicId)  

另一个示例:
/Topic/MySql-queries/AddSubTopic?topicId=1

调用:
控制器:主题
操作:添加子主题
topicId =1

这是 TopicConroller

public ActionResult AddSubTopic(int topicId)   

等中操作的签名(主题控制器中的所有操作仅接收一个参数 - 即主题的 id)。

How do you map this url
/Topic/topicName/action?topicId=someInt

Where Topic is the controller name (this is const - it always reads "Topic"),
topicName is being ignored
action is action name,
and someInt is the topicId argument for "action".

Here are couple of examples:
/Topic/c-sharp/AddQuestion?topicId=1

To invoke:
Controller: Topic
Action: AddQuestion
topicId =1

Where this is the action's singnature in TopicConroller

public ActionResult AddQuestion(int topicId)  

Another example:
/Topic/MySql-queries/AddSubTopic?topicId=1

To invoke:
Controller: Topic
Action: AddSubTopic
topicId =1

Where this is the action's singnature in TopicConroller

public ActionResult AddSubTopic(int topicId)   

etc. (all actions in Topic controller receive only one argument - that is the topic's id).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我不是你的备胎 2024-11-14 20:25:14
routes.MapRoute(
    "Topics",
    "Topic/{topicName}/{action}",
    new { controller = "Topic", action = "Index" }
);

如果您

public class TopicController: Controller
{
    public ActionResult AddQuestion(int topicId) 
    {
        ...
    }
}

关心主题名称,您可以让控制器操作将其作为操作参数:

public ActionResult AddQuestion(int topicId, string topicName) 
{
    ...
}
routes.MapRoute(
    "Topics",
    "Topic/{topicName}/{action}",
    new { controller = "Topic", action = "Index" }
);

where you would have:

public class TopicController: Controller
{
    public ActionResult AddQuestion(int topicId) 
    {
        ...
    }
}

and if you cared about the topic name you could have your controller action take it as action parameter:

public ActionResult AddQuestion(int topicId, string topicName) 
{
    ...
}
活雷疯 2024-11-14 20:25:14

我发现这个工具路由调试器对于测试我的完美路由非常有用

http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx

现在它还有一个 nuget 包。
也可能对进一步的场景有所帮助......

I found this tool route debuger very useful to test my way around to the perfect route

http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx

it has also a nuget package now.
Might help for further scenarios, too...

猫性小仙女 2024-11-14 20:25:14

你尝试过吗:

            routes.MapRoute(
                "Default",
                "{controller}/{topic}/{action}/{id}",
                new { controller = "Home", action = "Index" }
            );

另一种方法是像stackoverflow那样做。看一下问题 url,问题在 id 之后,可能会被忽略。

            routes.MapRoute(
                "Default",
                "{controller}/{action}/{id}/{*catchall}",
                new { controller = "Home", action = "Index" }
            );

Have you tried:

            routes.MapRoute(
                "Default",
                "{controller}/{topic}/{action}/{id}",
                new { controller = "Home", action = "Index" }
            );

Another way is to do it like stackoverflow. Take a look at question url, question is after id and is probably ignored.

            routes.MapRoute(
                "Default",
                "{controller}/{action}/{id}/{*catchall}",
                new { controller = "Home", action = "Index" }
            );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文