Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dimitris Chrysomallis 21 posts 73 karma points
    Apr 04, 2011 @ 17:01
    Dimitris Chrysomallis
    0

    request.Url in Macro property setter gives null reference exception

    Hello,

    We have user control in which we have a public property that sets the base url of hyperlinks.

    public string DestinationUrl
    {
    get { return _destinationUrl; }
    set
    {
    _destinationUrl = Request.Url.GetLeftPart(UriPartial.Authority) + "/" + value;
    }
    }

    problem is that this property is never set properly. Actually, Whatever reference to Request.Url properties (when referenced from inside a public property) results in null ref exception. This does not happen when the control is used outside umbraco.

    Is the Url object beinng reset?

    what is the proper way to pass host, authority, path, query properties to a user control through umbraco?

    Thank you

    Dimitris

  • Daniel Bardi 927 posts 2562 karma points
    Apr 04, 2011 @ 17:27
    Daniel Bardi
    0

    You can wrap the usercontrol as a macro and expose the public properties as macro parameters.

    This could solve your problem..   Pass the querystring values to the macro in a template.

  • Dimitris Chrysomallis 21 posts 73 karma points
    Apr 04, 2011 @ 19:02
    Dimitris Chrysomallis
    0

    Hi Daniel,

    Thanks for your response. I have already created the macro for the control.

    this is the macro:

    <umbraco:Macro  Alias="MySearchResults" DestinationUrl="test-page.aspx" runat="server"></umbraco:Macro>

    The trace gives me the following exception:

    macro.loadControlProperties Error adding property 'JobPageUrl' with value 'test-page.aspx', maybe it doesn't exists or maybe casing is wrong!
    Exception has been thrown by the target of an invocation.
    at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
    at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
    at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
    at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
    at umbraco.macro.loadUserControl(String fileName, MacroModel model, Hashtable pageElements)

    However, when i modify my control property to not reference Request.Url, it works fine.

    //This works

    public string JobPageUrl
    {
    get { return jobPageUrl; }
    set
    {
    jobPageUrl = value;
    }
    }
    //This throws exception
     
    public string JobPageUrl
    {
    get { return jobPageUrl; }
    set
    {
    jobPageUrl = Request.Url.GetLeftPart(UriPartial.Authority) + "/" + value;
    }
    }

    It seems as if the Request.url object is being referenced before initialization. This does not occur when running the control in a plain asp.net app.

    Any thoughts?

  • Daniel Bardi 927 posts 2562 karma points
    Apr 04, 2011 @ 22:40
    Daniel Bardi
    0

    Move the Request.Url code to the get and call the property JobPageUrl instead of the variable jobPageUrl from your code

    example:

    public string JobPageUrl
           
    {
               
    get { return Request.Url.GetLeftPart(UriPartial.Authority) + "/" + jobPageUrl; }
               
    set { jobPageUrl = value; }
           
    }

Please Sign in or register to post replies

Write your reply to:

Draft