November Happy Hour will be moved to Thursday December 5th.

Url.Action works on 1 page, fails on the other

Vote:
 

Hi,

I have copied a piece of markup from 1 page view to another but it won't work on the second page.

The markup in question is:

<a href='@Url.Action("MyMethod", "MyController", queryStringModel)'>

call stack is: 

Exception information: 
    Exception type: ArgumentNullException 
    Exception message: Value cannot be null.
Parameter name: stringToEscape
   at System.Uri.EscapeUriString(String stringToEscape)
   at EPiServer.Web.Routing.ContentRoute.AddQueryParameter(StringBuilder virtualPath, RouteValueDictionary values, String name, Boolean first)
   at EPiServer.Web.Routing.ContentRoute.SetQueryParameters(StringBuilder virtualPath, RouteValueDictionary values, HashSet`1 usedValues)
   at EPiServer.Web.Routing.ContentRoute.GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
   at System.Web.Routing.RouteCollection.GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
   at System.Web.Mvc.RouteCollectionExtensions.GetVirtualPathForArea(RouteCollection routes, RequestContext requestContext, String name, RouteValueDictionary values, Boolean& usingAreas)
   at System.Web.Mvc.UrlHelper.GenerateUrl(String routeName, String actionName, String controllerName, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, Boolean includeImplicitMvcValues)
   at System.Web.Mvc.UrlHelper.Action(String actionName, String controllerName, Object routeValues)

Setting breakpoints in 

EPiServer.Web.Routing.ContentRoute.GetVirtualPath and
EPiServer.Web.Routing.ContentRoute.SetQueryParameters

I have identified that on the page where the Url.Action succeeds, it executes EPiServer.Web.Routing.ContentRoute.GetVirtualPath but never enters the SetQueryPamaters.
Whereas on the page where it fails, it does execute SetQueryParameters ?

Any ideas why this method would produce different results on different pages ?
#86543
May 23, 2014 11:46
Vote:
 

** formatting got a bit messed up

After setting breakpoints 

I have identified that on the page where the Url.Action succeeds, it executes EPiServer.Web.Routing.ContentRoute.GetVirtualPath but never enters the SetQueryPamaters.

Whereas on the page where it fails, it does execute SetQueryParameters ?

Any ideas why this method would produce different results on different pages ?

For anyone else who may hit this issue, I believe the issue is due to the Action URL I was trying to render was the same page I was currently on.

If this is the case, then if carries on with processing in the EPiServer code and calls the SetQueryParameters which doesn't handle the null property values in this case.

If it was on different page, it doesn't carry on processing in the EPiServer code, instead I believe it returns a null and reverts to standard MVC Url.Action processing.

To overcome this I simple removed the queryStringModel and processed that myself.

var url = Url.Action("MyMethod", "MyController");
var queryString = string.Join("&", (new RouteValueDictionary(queryStringModel) as IEnumerable<KeyValuePair<string, object>>)
                        .Where(kvp => kvp.Value != null)
                        .Select(kvp => string.Format("{0}={1}", kvp.Key, HttpUtility.UrlEncode(kvp.Value.ToString()))));

var fullUrl = string.Format("{0}?{1}", url, queryString)
#86731
May 29, 2014 9:19
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.