【问题标题】:ASP.NET MVC Overriding Index action with optional parameter带有可选参数的 ASP.NET MVC 覆盖索引操作
【发布时间】:2016-11-17 04:08:53
【问题描述】:

我想接受/User//User/213123

其中213123是一个参数(user_id)

这是我的 RouteConfig.cs

            routes.MapRoute(
                name: "user",
                url: "{controller}/{action}/{username}",
                defaults: new { controller = "User", action = "Index", username = UrlParameter.Optional }
            );

还有我的UserController.cs

        public ActionResult Index()
        {
            ViewData["Message"] = "user index";
            return View();
        }

        [Route("user/{username}")]
        public ActionResult Index(string username)
        {
            ViewData["Message"] = "!" + username + "!";
            return View();
        }

这在 .net-core 1.0 中有效,但在 mvc5 中无效。我错过了什么?

谢谢

编辑:

在我的 UserController.cs 中只包含这个也不起作用(返回 404):

        public ActionResult Index(string username)
        {
            if (!String.IsNullOrEmpty(username))
            {
                ViewData["Message"] = "Hello " + username;
            }
            else
            {
                ViewData["Message"] = "user index";
            }

            return View();
        } 

描述:HTTP 404。您要查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请查看以下 URL 并确保其拼写正确。

请求的 URL:/user/asd

EDIT2:

更新了 RouteConfig.cs

        routes.MapRoute(
            "userParam", "user/{username}",
          new { controller = "user", action = "IndexByUsername" },
          new { username = @"\w+" }
        );

        routes.MapRoute(
            name: "user",
            url: "user",
            defaults: new { controller = "User", action = "Index"}
        );

/User/ 现在以 Index 作为用户名调用 IndexByUsername

/User/asd 仍然返回 404

EDIT4:当前代码:

RouteConfig.cs:

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

UserController.cs

public class UserController : Controller
{
    public ActionResult Index(string username)
    {
        if (!String.IsNullOrEmpty(username))
        {
            ViewData["Message"] = "Hello " + username;
        }
        else
        {
            ViewData["Message"] = "user index";
        }

        return View();
    }
}

【问题讨论】:

  • 你只需要public ActionResult Index(string username)(并检查username是否为null
  • @StephenMuecke 请看我的编辑
  • 你的 url 是 /user/asd 试图调用 userControllerasd() 方法 - 因此 404 不存在。它需要是 user/index/asd (或者你可以创建一个具体路线仅/user/{username})
  • @StephenMuecke 请查看我的最新编辑。
  • "user/{username}" 需要是 new { controller = "user", action = "Index" },(不是 action = IndexByUsername)。对于第二个,您只需要原来的路由定义,或者您可以将username = UrlParameter.Optional 添加到userParam 路由并删除user 路由

标签: asp.net-mvc asp.net-mvc-5 asp.net-mvc-routing .net-4.5.2


【解决方案1】:

您只需要一个带有签名的操作方法

public ActionResult Index(string username)

在该方法中,您可以检查username 的值是否为null

那你的路由定义需要是(注意user路由需要放在默认路由之前)

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

【讨论】:

    猜你喜欢
    • 2014-11-10
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多