使用 ASP.NET MVC HtmlHelper 扩展方法对链接 ID 进行 UrlEncode
我有一些操作采用基于用户名的字符串 id 参数,其中可以包含需要编码的字符,例如“user?1”
如果我使用 ActionLink()
生成链接,并传递字符串如果不进行编码,它会生成如下链接:http:\\localhost\controller\action\user?1
,并且该操作将“user”作为 id 传递。
如果我在将字符串传递给 ActionLink 之前将其 UrlEncode()
,则生成的链接为: http:\\localhost\controller\action\user%253f1< /code> 因为 ActionLink 将为您编码“%”字符。 除了看起来丑陋之外,当点击链接时,它还会生成一个 HTTP 错误 400 - 错误请求,但我还没有找到原因。
有什么方法可以生成如下网址:http:\\localhost\controller\action\user%3f1
?
I have actions that take string id parameters that are based on a username which can include characters that require encoding, for instance "user?1"
If I use ActionLink()
to generate the links, passing the string without encoding, it generates a link like this: http:\\localhost\controller\action\user?1
, and the action gets passed "user" as the id.
If I UrlEncode()
the string before passing it to ActionLink
, then the link generated is: http:\\localhost\controller\action\user%253f1
as ActionLink will then encode the '%' character for you. Besides this looking ugly, it then also generates a HTTP Error 400 - Bad Request when following the link which I've not yet tracked down the cause of.
Is there any way that I can generate the url like: http:\\localhost\controller\action\user%3f1
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
去掉怎么样? 字符或将其替换为破折号 (-) 或下划线 (_) 等其他内容?
How about removing the ? character or replacing it with something else like a dash (-) or underscore (_) ?
为了方便起见,您应该在 Global.asax.cs 文件中
添加另一个路由,在本例中为 ff。 可能有效:
我想这就是您想要的,为每个用户单独执行操作,但我建议您使用 cookie 来实现此目的。
PS:请记住将其放在默认路由的顶部,因为路由会尝试从上到下匹配。
You should look in the Global.asax.cs file
add another route for your convenience, in this case, the ff. might work:
I guess this is what you want, to separate action for each users, but i suggest you use cookie for this purpose.
PS: Remember to put that one on top of your default route since routing is trying to match from top to bottom.
您可以创建一个 jquery 插件来检查所有链接并将需要替换的字符替换为新值。
并将此插件应用到所有 ActionLinks 后
you can create a jquery plugin that check all the links and replace the char that you need to replace with the new value.
and after apply this plugin to all the ActionLinks