在 Asp.net MVC 中编辑 URL

Edit URL in Asp.net MVC

我想像这样在 url 中展示我的 link:

http://localhost:60000/Admin/myControlerName/myActionName/3/7/2

这是我的路由配置:

这是我的控制器:

这是我的操作link:

Mylink 在 url 中显示为:

http://localhost:60000/Admin/myControlerName/myActionName/?id1=3&id2=7&id3=2

但我想这样显示:

http://localhost:60000/Admin/myControlerName/myActionName/3/7/2

我哪里错了? :(

这里有 2 个不同的问题。首先,在一条路由上做3个可选参数是不行的。只有最后一个(最右边的)参数可以是可选的。因此,您需要两条路线来启用 URL.

中 0、1、2、3、4 或 5 段的所有组合
// This will match URLs 4 or 5 segments in length such as:
//
//   /Home/Index/2/3
//   /Home/Index/2/3/4
//
routes.MapRoute(
    name: "4-5Segments",
    url: "{controller}/{action}/{id}/{id2}/{id3}",
    defaults: new { id3 = UrlParameter.Optional }
);

// This will match URLs 0, 1, 2, or 3 segments in length such as:
//
//   /
//   /Home
//   /Home/Index/
//   /Home/Index/2
//
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

其次,您的 ActionLink 不匹配,因为您将路由值指定为 id1,但在您的路由中该值为 id。所以,你需要改变ActionLink如下。

@Html.ActionLink("my text", "myActionName", "myControllerName", new { id = 3, id2 = 7, id3 = 2}, null)

假设您已正确设置控制器:

public class myControllerNameController : Controller
{
    //
    // GET: /myActionName/

    public ActionResult myActionName(int id = 0, int id2 = 0, int id3 = 0)
    {
        return View();
    }

}

参见working demo here

For future reference, please provide the code as text. It is really hard to copy and edit an image, so you are much less likely to get an answer.