如何在asp.net mvc中为urtelink添加url参数?

时间:2022-08-30 10:24:34

All, my situation is that I have the basic route, plus some other simple routes:

所有,我的情况是我有基本的路线,加上一些其他简单的路线:

routes.MapRoute(
                "Default",                                              
                "{controller}/{action}/{id}",                           
                new { controller = "Home", action = "Index", id = 1}
            );

So the following url works: http://somesite.com/tags/index/1

以下网址有效:http://somesite.com/tags/index/1

However, some of my index pages take url parameters in the following fashion:

但是,我的一些索引页以下列方式获取url参数:

http://somesite.com/tags/index/1?when=lastmonth

How do I use Html.RouteLink to link to this?

如何使用Html.RouteLink链接到此?

You can't add '?' to routes in the global asax file like this:

你不能添加'?'到全局asax文件中的路由,如下所示:

routes.MapRoute("TagsWhen", "Tags/index/{id}?when={when}",
      new {controller = "Tags", action = "Index", id = "", when = ""});

If this route worked I could link to it using:

如果这条路线有效,我可以使用以下链接链接到它

Html.RouteLink(string.Format("{0} ", link.Rating), "LinksWhen", 
               new {id=link.ReferenceId, when=Model.When})

but it doesn't! So I'm not sure how I would use a Html.RouteLink to generate http://somesite.com/tags/index/1?when=lastmonth

但事实并非如此!所以我不确定如何使用Html.RouteLink生成http://somesite.com/tags/index/1?when=lastmonth

3 个解决方案

#1


Just found the solution myself. You can just do a regular Html.RouteLink and any object properties you don't have mapped to the url in global.asax it adds as a url parameter.

我自己找到了解决方案。你可以只做一个常规的Html.RouteLink和你没有映射到global.asax中的url的任何对象属性,它添加为url参数。

So using this route:

所以使用这条路线:

    routes.MapRoute(
        "Links",                                              
        "Links/details/{id}",                           
        new { controller = "Links", action = "Details", id = ""} defaults
    );

and this routelink:

这个routelink:

Html.RouteLink("Link Text", "Links", 
        new {id=link.ReferenceId, when=Model.When })

generates the correct url:

生成正确的网址:

http://localhost:2535/Links/details/1?when=onemonth

#2


Matthew's approach probably didn't work because he needed another null parameter on the end, otherwise it passes the route values as html attributes. :)

Matthew的方法可能不起作用,因为他最后需要另一个null参数,否则它将路由值作为html属性传递。 :)

<%= Html.ActionLink("Link text", "Index", "Home", new { id = 1, when = "lastmonth" }, null) %>

That last MapRoute() you came up with should work fine against it.

您提出的最后一个MapRoute()应该可以正常工作。

#3


I don't have a computer able to test this, but off the top of my head

我没有一台计算机可以测试这个,但是我的头顶

<%= Html.ActionLink("Link text", "Index", "Home", new { id = 1, when = "lastmonth" } %>

You don't need to specify optional parameters in your routes in the global.asax.cs file. Anything that doesn't match gets chucked into the query string by default.

您无需在global.asax.cs文件中的路由中指定可选参数。默认情况下,任何不匹配的内容都会被释放到查询字符串中。

#1


Just found the solution myself. You can just do a regular Html.RouteLink and any object properties you don't have mapped to the url in global.asax it adds as a url parameter.

我自己找到了解决方案。你可以只做一个常规的Html.RouteLink和你没有映射到global.asax中的url的任何对象属性,它添加为url参数。

So using this route:

所以使用这条路线:

    routes.MapRoute(
        "Links",                                              
        "Links/details/{id}",                           
        new { controller = "Links", action = "Details", id = ""} defaults
    );

and this routelink:

这个routelink:

Html.RouteLink("Link Text", "Links", 
        new {id=link.ReferenceId, when=Model.When })

generates the correct url:

生成正确的网址:

http://localhost:2535/Links/details/1?when=onemonth

#2


Matthew's approach probably didn't work because he needed another null parameter on the end, otherwise it passes the route values as html attributes. :)

Matthew的方法可能不起作用,因为他最后需要另一个null参数,否则它将路由值作为html属性传递。 :)

<%= Html.ActionLink("Link text", "Index", "Home", new { id = 1, when = "lastmonth" }, null) %>

That last MapRoute() you came up with should work fine against it.

您提出的最后一个MapRoute()应该可以正常工作。

#3


I don't have a computer able to test this, but off the top of my head

我没有一台计算机可以测试这个,但是我的头顶

<%= Html.ActionLink("Link text", "Index", "Home", new { id = 1, when = "lastmonth" } %>

You don't need to specify optional parameters in your routes in the global.asax.cs file. Anything that doesn't match gets chucked into the query string by default.

您无需在global.asax.cs文件中的路由中指定可选参数。默认情况下,任何不匹配的内容都会被释放到查询字符串中。