ActionLink contains a slash ('/') and breaks the link

I have a link to an action that looks like this:

<td>@Html.ActionLink(item.InterfaceName, "Name", "Interface", new { name = item.InterfaceName}, null)</td>

item.InterfaceNamegoing from the database and FastEthernet0/0. This leads to the creation of my HTML link to go to "localhost:1842/Interface/Name/FastEthernet0/0". Is there a way to make the "FastEthernet0/0"URL friendly so that my routing is not confused?

+1
source share
3 answers

You can get around this by replacing the slash.

ActionLink(item.InterfaceName.Replace('/', '-'), ....)

After that, your link will look like this: localhost:1842/Interface/Name/FastEthernet0-0. Naturally, your ActionMethod in your controller will be incorrect, because it will expect a well-named interface, therefore, by calling the method, you need to return a replacement:

public ActionResult Name(string interfaceName)
{
   string _interfaceName = interfaceName.Replace('-','/');
   //retrieve information
   var result = db.Interfaces...

}

:

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

}

+3

, name URL . , URL, URL.

0

Url.Encode, "/", , "? #%", URL-! Url.Encode , , :

http://www.w3schools.com/TAGs/ref_urlencode.asp

String.Replace, . :

<td>@Html.ActionLink(item.InterfaceName, "Name", "Interface", new { name = Url.Encode(item.InterfaceName)}, null)</td>

- .

public ActionResult Name(string interfaceName)
{
    //interfaceName is FastEthernet0/0
}

item.InterfaceName.Replace ('/', '-') is completely erroneous, for example, "FastEthernet-0/0" will be transmitted as "FastEthernet-0-0" and decoded to "FastEthernet / 0/0", which is incorrect.

0
source

Source: https://habr.com/ru/post/1531150/


All Articles