Asp.net MVC URL 路由:在有参数时更改 URL 时出现问题
本文关键字:URL 问题 参数 MVC net 路由 Asp | 更新日期: 2023-09-27 18:36:17
我需要更改MVC项目中的所有URL,到目前为止,这非常简单 - 我只需要更改所有适当路由中的URL字符串。global.asax.cs 文件中的 MapRoute()。
特别是一个环节表现得很固执,不会改变,我敢肯定,因为它是唯一有参数的环节。
有问题的地图路线是:
routes.MapRoute(
"Mortgage.GetThisRate",
"mortgage-rates/mortgage-rate-details/{groupId}/{paymentType}/{mortgageValue}/{province}",
new { controller = "Mortgage", action = "GetThisRate", paymentType = UrlParameter.Optional, mortgageValue = UrlParameter.Optional, province = UrlParameter.Optional },
new { groupId = "''d+", paymentType = "''d+", mortgageValue = "''d+", province = "''d+" }
);
调用此mapRoute的按钮是:
@Html.OmnitureButton( Url.Action("GetThisRate", new { groupId = info.GroupId }), "<span class='"payment'"></span><br />Get Details", new {@class = "orange"}, "events", "event3", "", "event3", "", "Mortgage LearnMore")
按下此按钮时,它请求的 URL 是:http://www.apps.mysite.net/Mortgage/GetThisRate/8/48/200000/1 - 似乎完全忽略了其 mapRoute 方法中的 URL 字符串。
我尝试将此mapRoute()放在global.asax.cs的顶部,以确保它不会被更高优先级的路由忽略,但同样的问题仍然存在。
任何了解 MVC 路由的人都能够弄清楚这里出了什么问题吗?
忘记了 groupId 参数,应该是这样的(已经测试并且工作正常)
routes.MapRoute(
"Mortgage.GetThisRate",
"mortgage-rates/mortgage-rate-details/{groupId}/{paymentType}/{mortgageValue}/{province}",
new { controller = "Mortgage", action = "GetThisRate", groupId = UrlParameter.Optional, paymentType = UrlParameter.Optional, mortgageValue = UrlParameter.Optional, province = UrlParameter.Optional },
new { groupId = "''d+", paymentType = "''d+", mortgageValue = "''d+", province = "''d+" }
要与帮助程序建立链接,您必须指定要使用的路由名称
@Html.RouteLink("title of link", "Mortgage.GetThisRate", new { groupId = "8", paymentType = "48", mortgageValue = "200000", province = "1" })
所以,你的链接看起来像...
yoursite/mortgage-rates/mortgage-rate-details/8/48/200000/1
我看到您正在使用自定义控件,因此可以使用@URL生成操作的 url。路由网址并提供给你的自定义控件,如html参数
@Url.RouteUrl("Mortgage.GetThisRate", new { groupId = "8", paymentType = "48", mortgageValue = "200000", province = "1" })
扩展答案示例案例,您需要一个标题为"Click Me!"的操作链接,href到控制器"home",操作"sayHello",类属性"some_css"和HTML ID "link_say_hello"溶液
@Html.ActionLink("click me!", "sayHello", "home", null, new { id="link_say_hello", @class = "some_css"})
网页输出
<a class="some_css" href="/home/sayHello" id="link_say_hello">click me!</a>
为什么 attibute 类有 @??? 因为类是一个保留字,编译器会抛出以下错误 编译器错误消息:CS1513:} 预期请记住。
太好了!但在这种情况下,您需要自定义路由,而不是默认的 {控制器}/{操作} 并与您的自定义帮助程序一起使用所以这是需要@URL的时候。路由链接并给出像 html 属性一样的 href
...new {href = @Url.RouteUrl("Mortgage.GetThisRate", new { groupId = "8", paymentType = "48", mortgageValue = "200000", province = "1", id="link_say_hello", @class = "some_css" })}
输出
<a class="some_css" href="/mortgage-rates/mortgage-rate-details/8/48/200000/1" id="link_say_hello">click me!</a>
看起来路由的名称"Mortgage.GetThisRate"与您在操作"GetThisRate"中使用的名称不匹配(也是自定义助手,因此可能会自动添加"抵押"。
解决方案是使用 Url.RouteUrl() 并将其返回值转储到 Html.OmnitureButton() 中。mapRoute() 可选参数需要更改为默认值,因为 RouteUrl() 不适用于可选参数。
工作代码如下所示:
routes.MapRoute(
"Mortgage.GetThisRate",
"mortgage/mortgage-rate-details/{groupId}/{paymentType}/{mortgageValue}/{province}",
new { controller = "Mortgage", action = "GetThisRate", paymentType = 48, mortgageValue = 200000, province = 1 },
new { groupId = "''d+", paymentType = "''d+", mortgageValue = "''d+", province = "''d+" }
);
和按钮:
@{
string x = Url.RouteUrl("Mortgage.GetThisRate", new { groupId = info.GroupId});
@Html.OmnitureButton(x, "<span class='"payment'"></span><br />Get Details", new {@class = "orange"}, "events", "event3", "", "event3", "", "Mortgage LearnMore")
}