locked
How do I stop the Image control from automatically UrlEncoding what I set to the ImageUrl parameter? RRS feed

  • Question

  • User-1052732201 posted

    I am generating a page while passing a link object. I set the property for "/get?"var1="+value1 + "&var2="+value2.

    this gets render as /file/Get%3fvar1%3dvalue1%26var2%3d6%value2 and it wont find the page.

    I try using server,UrlDecode with no success. Is there another way to code and generate the page?

    I populate a link control as : control class

     foreach (DateTime num in GDPDistinctYearMonthsDays)
                {
                    HyperLink linkData = new HyperLink();
                    linkData.ID = num.Year.ToString()+num.Month.ToString()+num.Day.ToString();
                    linkData.NavigateUrl = "/Get?" + "year=" + num.Year + "&month=" + num.Month + "&day=" + num.Day;
                    linkData.Text = "Whatever date " + num.Year + "-" + num.Month + "-" + num.Day;
                    linkData.Font.Bold = true;
                   
                    ListOfFilesLink.Add(linkData);
                }
               
                return View(ListOfFilesLink);
            }

    The Index page will get the collection

    <table>
    @foreach (var item in Model)
    {
     <tr>
        <td>
            @Html.ActionLink(item.Text, item.NavigateUrl) -------  this here generates % encoded value for ?/&.... If I click in the link it wont find the page.
         </td>
      </tr>
    }
    </table>               

    Tuesday, November 3, 2015 10:28 AM

Answers

  • User753101303 posted

    So the source problem is that Html.ActionLink(Text,ActionName) expects an action name, NOT a url as the second parameter. The action name is url encoded so that it can use any character someone might want to use. So you need instead to pass the action that is just "get" and then parameters as the 3rd parameter.

    Then I tried to follow your design where it seems that you create controls not used for themselves but as a source for "preprocessing" data (ie generating la link) then passed to the view.

    What I would really do is just pass "raw" data to the view and let the view use those data ie :

    // Test data
    var GDPDistinctYearMonthsDays = Enumerable.Range(1, 10).Select(value => DateTime.Today.AddDays(value));
    return View(GDPDistinctYearMonthsDays);
    

    then used this way:

    @foreach(var item in Model)
    {
        <li>
            @Html.ActionLink("Select "+item.ToString("d"),"get",new {year=item.Year,month=item.Month,day=item.Day})
        </li>
    }
    

    which generates a /file/get?year=2015&month=11&day=5 link rather than a /file/get%3fyear%3d2015%26month%3d11%26day%35 that is an action named "get?year=2015&month=11&day=5" with NO querystring parameters.

    Actually I might even generate rather a /file/get/2015/11/5 depending on the exact goal for this action.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 4, 2015 4:00 PM

All replies

  • User753101303 posted

    Hi,

    mjocasio

    another way

    But first which way have you done that? "/get?"var1="+value1 + "&var2="+value2 should not even compile as you have an extra " character plus it's not clear if you are doing that from JavasScript or code behind and this case by assigning NavigateUrl on an HyperLink control ???)

    Also could it be that you don't do that directly but use UrlEncode agains the value. For example :

    h.NavigateUrl=Server.UrlEncode("get?var1="+value1 + "&var2="+value2);

    would give the same problem. If this is what you used it should be rather something such as :

    h.NavigateUrl = "get?var1="+Server.UrlEncode(value1)+"&var2="+Server.UrlEncode(value2);

    Try to show the exact and full line of code that you are using.

    Edit: so it seems rather an incorrect usage of ActionLink you are using https://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink(v=vs.118).aspx#M:System.Web.Mvc.Html.LinkExtensions.ActionLink%28System.Web.Mvc.HtmlHelper,System.String,System.String%29 which expects just an action name rather than an url.

    Not sure how to best fix this as it seems you are half way between MVC and WebForms. The HyperLink controls are really used or is this used to avoid creating your own model? For now I would likely create my own model that would be more suitable to using the ActionLink (maybe rather https://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink(v=vs.118).aspx#M:System.Web.Mvc.Html.LinkExtensions.ActionLink%28System.Web.Mvc.HtmlHelper,System.String,System.String,System.Web.Routing.RouteValueDictionary%29 with route values ???)

    Tuesday, November 3, 2015 11:02 AM
  • User-1052732201 posted

    I use the controller feature (class) to generate a link object which I pass to the associate view for display as a collection of these objects (link)..... every link is a different query to the database.That part works fine...... Is the URL navigation property of the object which gets render and when I click on it does not fine the page. If I hard code the equivalent decoded value it find the page. Is there a way which I could prevent this property to be render prior to display.

    When I hovel and click on the link this is what appears in the status bar

    http://localhost:61191/folder/Get%3fyear%3d2013%26month%3d11%26day%3d7

    this is the error

    Server Error in '/' Application.


    The resource cannot be found.

                 Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.            
    Requested URL: /folder/Get?year=2013&month=11&day=7


    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.81.0

    If I copy the Requested URL above and paste in the status bar it works.....

    Tuesday, November 3, 2015 2:37 PM
  • User753101303 posted

    So what I'm trying to tell is that you pass "get?year=2013&month=11&day=7" where JUST the action name is expected. Compare the markup generated by:

                       @Html.ActionLink("You are using","get?year=2015&month=11&day=7")
                       @Html.ActionLink("You want","get",new {year=2015,month=11,day=7})
    

    Rather than using an HyperLink contro;, I would create my own model and would use Html.ActionLink(item.Text,item.ActionName,Item.Values); and so action name would be JUST "get" and values would be JUST the values. 

    Tuesday, November 3, 2015 6:20 PM
  • User-1052732201 posted

    Well according to the object oriented pattern, the model has anything to do with the User Interface, It is through the Controller Class, I am creating dynamically the Control object (in this case link objects) which are access thru the view.  That is working.... I am able to generate the control links. I can not make it the link functional due to the NavigateURL property which is render the & and = and ? while deploying the CSHTML You example is hardcode which I do not want.... Or if you could show me what you mean by adding a model will impose the controller and eventually render by the view.

    Wednesday, November 4, 2015 1:45 PM
  • User753101303 posted

    So the source problem is that Html.ActionLink(Text,ActionName) expects an action name, NOT a url as the second parameter. The action name is url encoded so that it can use any character someone might want to use. So you need instead to pass the action that is just "get" and then parameters as the 3rd parameter.

    Then I tried to follow your design where it seems that you create controls not used for themselves but as a source for "preprocessing" data (ie generating la link) then passed to the view.

    What I would really do is just pass "raw" data to the view and let the view use those data ie :

    // Test data
    var GDPDistinctYearMonthsDays = Enumerable.Range(1, 10).Select(value => DateTime.Today.AddDays(value));
    return View(GDPDistinctYearMonthsDays);
    

    then used this way:

    @foreach(var item in Model)
    {
        <li>
            @Html.ActionLink("Select "+item.ToString("d"),"get",new {year=item.Year,month=item.Month,day=item.Day})
        </li>
    }
    

    which generates a /file/get?year=2015&month=11&day=5 link rather than a /file/get%3fyear%3d2015%26month%3d11%26day%35 that is an action named "get?year=2015&month=11&day=5" with NO querystring parameters.

    Actually I might even generate rather a /file/get/2015/11/5 depending on the exact goal for this action.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 4, 2015 4:00 PM
  • User-1052732201 posted

    Thank you for your time.....

    Friday, November 6, 2015 10:56 AM