Last post Jul 11, 2012 02:04 AM by vinneyk
Jul 08, 2012 09:15 PM|vinneyk|LINK
I have an MVC 3 project which is running ASP.NET 4.5 RC and making use of the WebAPI. To be explicit, I am making an ajax call to the WebAPI controller from within the view. I would like my service results to include a URL to another page within the site.
In order to generate this url, I'm using UrlHelper.RouteUrl(routeDictionary).
This works fine for the first run after the application has been started. However, subsequent runs will result in a NullReferenceException. If anyone knows how to resolve this issue--or if anyone has a suggestion as to how I should be building routes differently,
please let me know.
at System.Web.HttpServerVarsCollection.Get(String name) at System.Web.Mvc.UrlRewriterHelper.WasThisRequestRewritten(HttpContextBase httpContext) at System.Web.Mvc.UrlRewriterHelper.WasRequestRewritten(HttpContextBase httpContext) at System.Web.Mvc.PathHelpers.GenerateClientUrlInternal(HttpContextBase
httpContext, String contentPath) at System.Web.Mvc.PathHelpers.GenerateClientUrl(HttpContextBase httpContext, String contentPath) 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.GenerateUrl(String routeName, String actionName, String controllerName, String protocol, String hostName, String fragment,
RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, Boolean includeImplicitMvcValues) at System.Web.Mvc.UrlHelper.RouteUrl(RouteValueDictionary routeValues) at MiniMilestones.Members.Web.Helpers.ArticleUrlHelper.BuildDetailsUrl(ArticleSummaryDTO
articleSummary) in c:\Users\VinneyK\My Repositories\Mercurial\Mini Milestones\MiniMilestones.Members.Web\Helpers\ArticleUrlHelper.cs:line 26 at MiniMilestones.Members.Web.Controllers.API.v1_0.Controllers.ArticlesController.<applyViewModelCasting>b__3(ArticleSummaryDTO
a) in c:\Users\VinneyK\My Repositories\Mercurial\Mini Milestones\MiniMilestones.Members.Web\Controllers\API\1.0\ArticlesController.cs:line 136 at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1
collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at MiniMilestones.Members.Web.Controllers.API.v1_0.Controllers.ArticlesController.applyViewModelCasting(IEnumerable`1 articles) in c:\Users\VinneyK\My Repositories\Mercurial\Mini
Milestones\MiniMilestones.Members.Web\Controllers\API\1.0\ArticlesController.cs:line 136 at MiniMilestones.Members.Web.Controllers.API.v1_0.Controllers.ArticlesController.getArticles(Expression`1 filterPredicate, Int32 skipCount, Int32 pageSize) in c:\Users\VinneyK\My
Repositories\Mercurial\Mini Milestones\MiniMilestones.Members.Web\Controllers\API\1.0\ArticlesController.cs:line 125
Here's the code which is called by the WebAPI method:
var slug = SlugBuilder.BuildSlug(articleSummary.Title);
var action = MVC.Articles.ArticleDetails(articleSummary.ID, slug);
var url = UrlHelper.RouteUrl(action.GetRouteValueDictionary()); // <-- throws
Jul 09, 2012 01:05 PM|Mudasir.Khan|LINK
is just one url not working or its not working for any url meaning for any other request then there should be some config issues please check
Jul 10, 2012 01:51 AM|vinneyk|LINK
It appears to be irrespective of data. For example, if I run the app fresh, load the page in question, and the refresh the page in attempt to load up the exact same data which was just rendered, this error will occur. Does that still seem like a configuration
issue to you?
While looking at the MVC code, I did notice a significant difference between the new UrlHelper in MVC4 RC vs the one in MVC3. I'm working on converting the app to MVC4 to see if this doesn't help.
Thanks for your response.
Jul 11, 2012 02:04 AM|vinneyk|LINK
So, this issue appears to have been due to a static readonly field which was of type UrlHelper. Here's to bad code:
public static class ArticleUrlHelper
public readonly static UrlHelper UrlHelper;
UrlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
public static string BuildDetailsUrl(this ArticleSummaryDTO articleSummary)
var action = MVC.Articles.ArticleDetails(articleSummary.ID, "");
var url = UrlHelper.RouteUrl(action.GetRouteValueDictionary());
While I don't fully understand the implications of static on the compiler, I'm making the assumption that this was failling because the static field UrlHelper remained in scope while the HttpContext which was used to construct the the object has since been
disposed. I was able to fix the problem by creating my UrlHelper variable in the method body which helps to enusre the availability of the RequestContext.