简体   繁体   English

在Asp.net MVC中编辑URL

[英]Edit URL in Asp.net MVC

I want to show my link in url like that: 我想像这样在网址中显示我的链接:

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

This is my routConfig: 这是我的routConfig:

在此处输入图片说明

This is my contoller: 这是我的contoller:

在此处输入图片说明

This is my action link: 这是我的动作链接:

在此处输入图片说明

Mylink shows in url like that: Mylink在网址中显示如下:

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

But i want to show like that: 但我想这样显示:

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

Where is my wrong? 我哪里错了? :( :(

You have 2 different issues here. 您在这里有2个不同的问题。 First of all, it won't work right to make 3 optional parameters on a route. 首先,在路由上设置3个可选参数将不起作用。 Only the last (right most) parameter can be optional. 只有最后一个(最右边)参数可以是可选的。 So, you need two routes to enable all of the combinations of 0, 1, 2, 3, 4, or 5 segments in the URL. 因此,您需要两条路由来启用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 }
);

Secondly, your ActionLink is not matching because you have specified the route value as id1 , but in your route the value is id . 其次,您的ActionLink不匹配,因为您已将路由值指定为id1 ,但在路由中的值是id So, you need to change ActionLink as follows. 因此,您需要按以下方式更改ActionLink

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

This assumes you have correctly set up your controller: 这假设您已经正确设置了控制器:

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

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

}

See the 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. 复制和编辑图像确实很困难,因此您获得答案的可能性要小得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM