ActionLink包含斜杠('/')并断开链接 [英] ActionLink contains slash ('/') and breaks link
问题描述
我有一个如下的动作链接:
I have an Action Link that's as follows:
<td>@Html.ActionLink(item.InterfaceName, "Name", "Interface", new { name = item.InterfaceName}, null)</td>
item.InterfaceName
是从数据库中收集的,并且是 FastEthernet0/0
.这导致我的HTML链接被创建为导致"localhost:1842/Interface/Name/FastEthernet0/0"
.有没有一种方法可以使"FastEthernet0/0"
URL友好,以使我的路由不会引起混淆?
item.InterfaceName
is gathered from a database, and is FastEthernet0/0
. This results in my HTML link being created to lead to "localhost:1842/Interface/Name/FastEthernet0/0"
. Is there a way to make "FastEthernet0/0"
URL friendly so that my routing doesn't get confused?
推荐答案
您可以通过替换斜杠来解决此问题.
You could work around this, by replacing the slash.
ActionLink(item.InterfaceName.Replace('/', '-'), ....)
此后,您的链接将如下所示: localhost:1842/Interface/Name/FastEthernet0-0
.自然,您的控制器中的ActionMethod行为不当,因为它将期望使用一个命名良好的接口,因此在调用该方法时,您需要还原replace:
After this your link would look like this: localhost:1842/Interface/Name/FastEthernet0-0
.
Naturally, your ActionMethod in your controller will misbehave, because it will expect a well named interface, so upon calling the method you need to revert the replace:
public ActionResult Name(string interfaceName)
{
string _interfaceName = interfaceName.Replace('-','/');
//retrieve information
var result = db.Interfaces...
}
一种替代方法是构建自定义路由来捕获您的请求:
An alternative would be to build a custom route to catch your requests:
routes.MapRoute(
"interface",
"interface/{*id}",
new { controller = "Interface", action = "Name", id = UrlParameter.Optional }
);
Your method would be:
public ActionResult Name(string interfaceName)
{
//interfaceName is FastEthernet0/0
}
Darin Dimitrov在此处提出了这种解决方案a>
This solution was suggested by Darin Dimitrov here
这篇关于ActionLink包含斜杠('/')并断开链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!