MVC 路由无法按需工作
MVC routing not working as needed
我正在尝试在 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 }
);
}
}
这就是我认为的link:-
@Html.ActionLink("Inbox", "inbox")
问题是输出 html 被生成为 http://localhost/Home/inbox
我只希望它是 http://localhost/inbox
感谢您的帮助
您使用了 Html.ActionLink
的错误重载。对于您使用的版本,它使用当前上下文来确定控制器,而您想要使用 Mail
控制器。因此,要解决此问题,请将其更改为明确指定控制器,如下所示:
@Html.ActionLink("Inbox", "Inbox", "Mail")
我正在尝试在 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 }
);
}
}
这就是我认为的link:-
@Html.ActionLink("Inbox", "inbox")
问题是输出 html 被生成为 http://localhost/Home/inbox
我只希望它是 http://localhost/inbox
感谢您的帮助
您使用了 Html.ActionLink
的错误重载。对于您使用的版本,它使用当前上下文来确定控制器,而您想要使用 Mail
控制器。因此,要解决此问题,请将其更改为明确指定控制器,如下所示:
@Html.ActionLink("Inbox", "Inbox", "Mail")