简体   繁体   English

如何在自定义ASP.NET MVC路由中使用String.Replace方法?

[英]How do I use String.Replace method inside a custom ASP.NET MVC Route?

I'm still a bit new to ASP.Net MVC and Custom Routing and I'm not quite sure how to ask this question other than to post the URL link and the route that I'm using and ask for advice. 我对ASP.Net MVC和“自定义路由”还是有点陌生​​,除了发布URL链接和我正在使用的路由并寻求建议外,我不太确定如何问这个问题。

The problem is: 问题是:

A potentially dangerous Request.Path value was detected from the client (&). 从客户端(&)中检测到潜在危险的Request.Path值。

This is being caused by an "&" symbol inside my link: 这是由于我链接中的“&”符号引起的:

http://localhost/search/cars-&-motorcycles

And, here's my route: 而且,这是我的路线:

routes.MapRoute(
    "CategorySearch",
    "category/{searchcriteria}",
    new { controller = "Listing", action = "Index", isCategory = true, searchcriteria = UrlParameter.Optional }
);

What I want to do is strip out the "&" from the optional paramter before it gets passed, hopefully that will correct the "potentially dangerous" issue that I'm experiencing. 我想做的是在可选参数通过之前删除“&”,希望可以解决我遇到的“潜在危险”问题。 I've tried to use 我尝试使用

UrlParameter.Optional.ToString().Replace("&", "")

Is it even possible to strip out that "&" symbol from somewhere inside the Route.MapRoute method at all? 甚至有可能完全从Route.MapRoute方法内部的某个位置删除该“&”符号吗?

您可以使用HtmlHelper.Encode对其进行编码:

string encodedUrl = HtmlHelper.Encode("http://localhost/search/cars-&-motorcyles");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM