在URL ASP.NET MVC中隐藏操作名称?

时间:2017-08-13 22:45:39

标签: asp.net-mvc routes

UsersControllerDetails操作在系统标识中具有用户的id(字符串)参数。

如何创建MapRoute,看看:

http://localhost:54719/Users/bade9105-adfe-46b3-b0db-68f3f6e0dd13

而不是:

http://localhost:54719/Users/Details/bade9105-adfe-46b3-b0db-68f3f6e0dd13

并执行自动重定向,而不仅仅是在不输入控制器名称的情况下进行访问。

1 个答案:

答案 0 :(得分:0)

Detail不是您的控制器名称,而是一个操作名称 试试这个: (添加到您的RoutingConfig.cs

  routes.MapRoute(
  name: "userdetail",
  url: "Users/{userid}",
  defaults: new
  {
      controller = MVC.Users.Name,
      action = MVC.Users.ActionNames.showuserdetail,
      userid = UrlParameter.Optional
  },
  namespaces: new[] { string.Format("{0}.Controllers", 
 typeof(RoutingConfig).Namespace) }
 );

(showuserdetail是你的行动名称)

您需要使用路由来执行您想要的操作。