Tuesday, March 26, 2019

URL.Action return &in URL

I recently encountered one issue that URL.Action returns & in URL,
Basically URL.Action method, which is used to resolve the qualified URL on based on Controller, Action Name with Route variable data, returns the qualified URL

Here is source code

HTML View:

  <script type="text/javascript">
$.OrderDetailsURL =  '@Url.Action("OrderDetails", "Order", new { orderID =OrderID, Status =Updated })';
  </script>

When you see javaScript variable OrderDetailsURL  value which have &amp ; in place of &


and we are expecting @Url.Action  method should return below URL without &amp ;


There are two ways to overcome this issue.

 1. Use @HTML.Row() – it returns string which are not HTML encoded. 

$.OrderDetailsURL =  '@Html.Raw(@Url.Action("OrderDetails", "Order", new { orderID =OrderID, Status =Updated }))';

 2In JavaScript, Call replace method to update &amp with &

          document.location.href = $. OrderDetailsURL.replace("&","&");

Thanks for visiting!!

No comments:

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...