How to URL encode parameters in ASP .NET MVC

前端 未结 3 1105
星月不相逢
星月不相逢 2020-12-04 01:52

I have the following code in my view:

<%= Html.ActionLink(
           \"View item\", 
           \"Index\", 
            \"Items\", 
            new 
             


        
3条回答
  •  囚心锁ツ
    2020-12-04 02:36

    You should be able to just use the UrlHelper instance of your view to do this for you. Try giving this a shot:

    <%= Html.ActionLink( "View item", "Index", "Items", new { itemName = Url.Encode(Model.ItemName) }, null) %>

    Update

    After testing, it seems explicitly encoding like I did above seems to be less accurate and will cause the server to double-encode (eg - % will come out as %2525 in the url).

提交回复
热议问题