简体   繁体   中英

Passing Parameter to URL ASP.NET MVC

I was just wondering what the best practice is for passing a parameter into a URL that contains characters that you do not wish to pass to the URL. For example, I pass the string /LM/W3SVC/7/ROOT into the URL but I want to pass it without the backslashes, thus like LMW3SVC7ROOT

The way I currently do this is by using .Replace() . This is the full line of code that I currently use <a href="@Url.Action("Errors", new { id=item.Application.Replace("/", "").Replace(".", "")})"> @Html.DisplayFor(modelItem => item.Application) </a> which works fine but throws out a warning every now and again so I was just wondering if this is the correct way of implementing it.

'Possible unintended reference comparison; to get a value comparison, cast the right hand side to type 'string'

Thanks in advance.

The problem you're getting is from var queryString = RouteData.Values["id"]; . If you look in Visual Studio and hover over var it will tell you the type of that particular var , which in this case is Object .

Try:

var queryString = RouteData.Values["id"].ToString();

In this case, you're finding yourself having to replace certain characters in your string, which probably means server side, you might be having to do some extra logic to cope without having these characters on the received string. In your View, just wrap the id in Url.Encode :

id = Url.Encode(item.Application)

You will receive this server side with the string decoded, ie /LM/W3SVC/7/ROOT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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