我需要指导从我的网址中删除Modulos标志。我希望我的路由完全如下所示。
stackoverflow.com/questions/15881575/get-the-selected-value-of-a-dropdownlist-asp-net-mvc
我的路由返回了一些内容
stackoverflow.com/questions/15881575/get%20the%20selected%20value%20of%20a%20dropdownlist-asp-net-mvc。
注意%20返回。请问我该如何获得连字符?
以下是我的路由配置。
routes.MapRoute(
name: null,
url: "{controller}/{action}/{id}/{title}",
defaults: new
{
controller = "Home",
action = "Index",
title=UrlParameter.Optional,
id = UrlParameter.Optional
}
);
Title是我的action方法的字符串参数。我想这样做:
routes.MapRoute(
name: null,
url: "{controller}/{action}/{id}/{title}",
defaults: new
{
controller = "Home",
action = "Index",
title=UrlParameter.Optional.ToString().Replace(' ','-'),
id = UrlParameter.Optional
}
);
然而它失败了。我或者尝试了
title=UrlParameter.Optional.ToString().Replace("%20","-"),
它也失败了。我是新手,并试图让它变得更好。请任何帮助将不胜感激。
答案 0 :(得分:6)
%20
是空间的URL编码版本。您无法使用路由定义解决此问题,您必须首先修改生成链接的代码,以便删除空格。
例如,如果您当前正在使用以下方式生成链接:
@Html.ActionLink(item.Title, "Details", new {
id = item.Id,
title = item.Title
})
您可以将其更改为
@Html.ActionLink(item.Title, "Details", new {
id = item.Id,
title = item.Title.Replace(" ", "-")
})
请注意,您可能需要处理除空格之外的其他角色,因此您可能最好使用:
title = Regex.Replace(item.Title, "[^A-Za-z0-9]", "")
答案 1 :(得分:0)
我的偏好是将其变为POST。然后你就不会编写任何将任何东西转换成其他东西的代码。