【问题标题】:MVC 5 Route Url Not WorkingMVC 5 路由 URL 不起作用
【发布时间】:2018-03-11 17:36:56
【问题描述】:

我有一个 UserController,其中包含以下操作:RegisterLoginUserProfile

所以对于这些操作,我希望 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


【解决方案1】:

您显示的所有 3 个 url 都与第一个路由匹配(这意味着任何包含 0 到 3 个段的 url),并且您的第 3 个 url(例如 ../User/OShai)采用 OShai()OShai() 方法 UserController 不存在。

您需要以正确的顺序定义特定的路线(第一场比赛获胜)

routes.MapRoute(
    name: "Register",
    url: "/User/Register",
    defaults: new { area = "", controller = "User", action = "Register" },
    namespaces: new[] { "MvcApplication" }
);
routes.MapRoute(
    name: "Login",
    url: "/User/Login",
    defaults: new { area = "", controller = "User", action = "Login" },
    namespaces: new[] { "MvcApplication" }
);
// Match any url where the 1st segment is 'User' and the 2nd segment is not 'Register' or 'Login'
routes.MapRoute(
    name: "Profile",
    url: "/User/{username}",
    defaults: new { area = "", controller = "User", action = "UserProfile" },
    namespaces: new[] { "MvcApplication" }
);
// Default
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { area = "", controller = "Home", action = "Index", id = UrlParameter.Optional },
    namespaces: new[] { "MvcApplication" }
);

Profile 路由匹配的位置

public ActionResult UserProfile(string username)

UserController

或者,您可以删除RegisterLogin 路由,并为Profile 路由创建一个约束以检查第二段是否匹配“注册”或“登录”,如果是,则返回 false 所以然后它匹配Default 路由。

public class UserNameConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        List<string> actions = new List<string>() { "register", "login" };
        // Get the username from the url
        var username = values["username"].ToString().ToLower();
        // Check for a match
        return !actions.Any(x => x.ToLower() == username);
    }
}

然后将Profile路由修改为

routes.MapRoute(
    name: "Profile",
    url: "/User/{username}",
    defaults: new { area = "", controller = "User", action = "UserProfile" },
    constraints: new { username = new UserNameConstraint() }
    namespaces: new[] { "MvcApplication" }
);

【讨论】:

    猜你喜欢
    • 2017-12-20
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多