Sperick
Sperick

Reputation: 2761

Url is not a valid virtual path

I have a hyperlink that passes variables with it. This is generating using a combination of strings and server properties. However it seems to work with some variables but not others.

If I pass the groupId and issuedMemberId then it's fine.

<asp:HyperLink runat="server" ID="groupUrlLink" NavigateUrl=<%# string.Concat("~/UpdateMember.aspx?groupId=", DataBinder.Eval(Container.DataItem, "GroupID").ToString(),"&issuedMemberId=", DataBinder.Eval(Container.DataItem, "IssuedMemberId").ToString()%> >

However if I instead use a different variable, Effective date,

<asp:HyperLink runat="server" ID="groupUrlLink" NavigateUrl=<%# string.Concat("~/UpdateMember.aspx?groupId=", DataBinder.Eval(Container.DataItem, "GroupID").ToString(),"&effectiveDate=",DataBinder.Eval(Container.DataItem, "EffectiveDate").ToString())%> >

I get an error:

'~/UpdateMember.aspx?groupId=0032409901&effectiveDate=3/31/2008 12:00:00 AM' is not a valid virtual path.

As can be seen in the error it is correctly using the value for the Effective date. This is the only thing to have changed though from the first example so I'm not sure why it isn't working now.

Upvotes: 3

Views: 10507

Answers (4)

Murdock
Murdock

Reputation: 4662

It's probably the forward slashes (in the parameter section of the URL) or the space. It is not valid characters for an URL.

Use HttpUtility.UrlEncode to encode the URL. Or as the comment below specifies.

Upvotes: 5

Damith
Damith

Reputation: 63065

you can send datetime as Convert.ToDateTime(DataBinder.Eval(Container.DataItem, "EffectiveDate")).ToString("yyyyMMddHHmmss")

then you can again convert query string value back to datetime using DateTime.ParseExact(dateString, "yyyyMMddHHmmss")

Upvotes: 1

fourpastmidnight
fourpastmidnight

Reputation: 4234

To add to the answer above, the OP should probably use a WebUtility.HttpEncode(...) call around the string passed to the NavigateUrl property of the Hyperlink:

<asp:HyperLink runat="server" ID="groupUrlLink" NavigateUrl=<%# WebUtility.UrlEncode(string.Concat("~/UpdateMember.aspx?groupId=", DataBinder.Eval(Container.DataItem, "GroupID").ToString(),"&effectiveDate=",DataBinder.Eval(Container.DataItem, "EffectiveDate").ToString())) %> >

UPDATE: In general, you should always make sure you encode data sent via URL to ensure that malicious input doesn't lead to some sort of script injection, cross-site scripting attack, or other nefarious intents.

Upvotes: 2

Satpal
Satpal

Reputation: 133403

Use HttpUtility.UrlEncode or Ticks when passing DateTime. I would suggest you to pass Ticks when passing DateTime in query string. Ticks is of long data type which can be converted to DateTime.

<asp:HyperLink runat="server" ID="groupUrlLink" NavigateUrl=<%# string.Concat("~/UpdateMember.aspx?groupId=", DataBinder.Eval(Container.DataItem, "GroupID").ToString(),"&effectiveDate=",DataBinder.Eval(Container.DataItem, "EffectiveDate").Ticks.ToString())%> >

Upvotes: 2

Related Questions