带有可选参数的 ASP.NET MVC 覆盖索引操作 [英] ASP.NET MVC Overriding Index action with optional parameter

查看:23
本文介绍了带有可选参数的 ASP.NET MVC 覆盖索引操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想接受 /User//User/213123

其中 213123 是一个参数 (user_id)

这是我的RouteConfig.cs:

 routes.MapRoute(名称:用户",url: "{controller}/{action}/{username}",默认值:new { controller = "User", action = "Index", username = UrlParameter.Optional });

还有我的UserController.cs:

 public ActionResult Index(){ViewData["Message"] = "用户索引";返回视图();}[路由(用户/{用户名}")]公共 ActionResult 索引(字符串用户名){ViewData["Message"] = "!"+ 用户名 + "!";返回视图();}

这适用于 .net-core 1.0 但不适用于 mvc5.我错过了什么?

谢谢

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

 public ActionResult Index(string username){if (!String.IsNullOrEmpty(username)){ViewData["Message"] = "你好" + 用户名;}别的{ViewData["Message"] = "用户索引";}返回视图();}

<块引用>

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

请求的网址:/user/asd

编辑 2:

更新了RouteConfig.cs:

 routes.MapRoute("userParam", "user/{username}",新{控制器=用户",动作=IndexByUsername"},新 { 用户名 = @"w+" });路线.MapRoute(名称:用户",网址:用户",默认值:新{控制器=用户",动作=索引"});

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

/User/asd 仍然返回 404

EDIT4:当前代码:

RouteConfig.cs:

routes.MapRoute(名称:默认",网址:{控制器}/{动作}/",默认值:新 { 控制器 =主页",动作 =索引"});route.MapRoute("userParam", "user/{username}", new { controller = "user", action = "Index"});route.MapRoute("user", "{controller}/{action}/{username}", new { controller = "User", action = "Index", username = UrlParameter.Optional });

UserController.cs:

公共类 UserController : 控制器{公共 ActionResult 索引(字符串用户名){if (!String.IsNullOrEmpty(username)){ViewData["Message"] = "你好" + 用户名;}别的{ViewData["Message"] = "用户索引";}返回视图();}}

解决方案

你只需要一个带有签名的action方法

public ActionResult Index(string username)

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

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

routes.MapRoute(名称:用户",网址:用户/{用户名}",默认值:new { controller = "User", action = "Index", username = UrlParameter.Optional });路线.MapRoute(名称:默认",url: "{controller}/{action}/{id}",默认值:新 { 控制器 =主页",动作 =索引",id = UrlParameter.Optional });

I want to accept /User/ and /User/213123

Where 213123 is a parameter (user_id)

Here is my RouteConfig.cs:

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

And my UserController.cs:

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

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

This works in .net-core 1.0 but not in mvc5. What am I missing?

Thank you

EDIT:

Having just this in my UserController.cs also doesn't work (returns 404):

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

            return View();
        } 

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /user/asd

EDIT2:

Updated 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/ now calls IndexByUsername with Index as the username

/User/asd still returns 404

EDIT4: Current code:

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();
    }
}

解决方案

You need only one action method with the signature

public ActionResult Index(string username)

and in that method you can check if the value of username is null or not.

Then you route definitiosn needs to be (note the user route needs to be placed before the default route)

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 }
);

这篇关于带有可选参数的 ASP.NET MVC 覆盖索引操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆