【发布时间】:2018-03-11 17:36:56
【问题描述】:
我有一个 UserController,其中包含以下操作:Register、Login 和 UserProfile。
所以对于这些操作,我希望 URL 是:
Register- /用户/注册
Login- /用户/登录
UserProfile- /User/{username} (这条路由只有在 未找到任何操作)
这就是我的 RouteConfig.cs 的样子:
// Default:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { area = "", controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MvcApplication" }
);
// User Profile - Default:
routes.MapRoute(
name: "UserProfileDefault",
url: "User/{username}",
defaults: new { area = "", controller = "User", action = "UserProfile" },
namespaces: new[] { "MvcApplication" }
);
我需要UserProfile 的路由只有在UserController 中没有任何操作来控制时才会控制。
不幸的是,我的代码不起作用,我得到一个 404 用于导航
到 UserProfile 路由,但所有其他 UserController 操作都在工作。
我还将UserProfile 路由移到顶部但仍然无法正常工作,我尝试了一切,似乎没有任何工作。
【问题讨论】:
-
Action 中传入的参数是什么样的?你能发一下吗?你直接去 /User 之后不使用任何东西吗?您是否尝试过 User/MyUserName 以查看它是否会触发操作?
标签: asp.net-mvc routes asp.net-mvc-5