【问题标题】:MVC routing not working as neededMVC 路由无法按需要工作
【发布时间】:2015-06-23 08:38:14
【问题描述】:

我正在尝试在 MVC 中设置自定义路由,但遇到了一些问题。

这是目前的配置文件:-

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


        routes.MapRoute(
            name: "inbox",
            url: "inbox",
            defaults: new { controller = "Mail", action = "Inbox", id = UrlParameter.Optional }
        );


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

    }
}

这是我认为的链接:-

@Html.ActionLink("Inbox", "inbox")

问题是输出 html 被生成为http://localhost/Home/inbox

我只希望它是http://localhost/inbox

感谢您的帮助

【问题讨论】:

  • 好人,成功了!

标签: model-view-controller asp.net-mvc-routing


【解决方案1】:

您使用了错误的 Html.ActionLink 重载。对于您使用的版本,它使用当前上下文来确定控制器,而您想使用 Mail 控制器。因此,要解决此问题,请将其更改为显式指定控制器,如下所示:

@Html.ActionLink("Inbox", "Inbox", "Mail")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-21
    相关资源
    最近更新 更多