Url.Route method throwing exceptionhttp://forums.asp.net/t/1789334.aspx/1?+Url+Route+method+throwing+exceptionFri, 06 Apr 2012 20:04:27 -040017893344916782http://forums.asp.net/p/1789334/4916782.aspx/1?+Url+Route+method+throwing+exception Url.Route method throwing exception <p>Hi,</p> <p>I have the following code that is throwing an exception(Object reference not set to an instance of an object.) on the line below.</p> <pre class="prettyprint">string uri = Url.Route(&quot;http://whatever/&quot;, new { id = contact.Id });</pre> <p>Anyone know why this does not work?<br />Thanks</p> <pre class="prettyprint">public HttpResponseMessage&lt;Product&gt; Post(Product product) { _productsRepository.Add(product); var response = new HttpResponseMessage&lt;Product&gt;(product, HttpStatusCode.Created); string uri = Url.Route(&quot;http://whatever/&quot;, new { id = contact.Id }); response.Headers.Location = new Uri(Request.RequestUri, uri); return response; }</pre> <p><br> <br> </p> <p></p> 2012-04-04T21:48:32-04:004916785http://forums.asp.net/p/1789334/4916785.aspx/1?Re+Url+Route+method+throwing+exceptionRe: Url.Route method throwing exception <p>Is <em>contact</em>&nbsp;a private member that could potentially be null? Or does the stacktrace show the exception being thrown in the Route method?</p> 2012-04-04T21:52:10-04:004916795http://forums.asp.net/p/1789334/4916795.aspx/1?Re+Url+Route+method+throwing+exceptionRe: Url.Route method throwing exception <p>StackTrace is what I have giving already - there is no inner exception. Contact is public is no problem there.</p> 2012-04-04T22:10:05-04:004916801http://forums.asp.net/p/1789334/4916801.aspx/1?Re+Url+Route+method+throwing+exceptionRe: Url.Route method throwing exception <p>I don't see the exception stacktrace. All I see is the exception message in your post.</p> 2012-04-04T22:14:31-04:004916808http://forums.asp.net/p/1789334/4916808.aspx/1?Re+Url+Route+method+throwing+exceptionRe: Url.Route method throwing exception <p>Sorry,StackTrace:</p> <p><strong></strong>at System.Web.Http.ApiController.get_Url()</p> <p>at TestApp.Controllers.ProductController.Post(RateSet newRateSet) in D:\TestApp\Controllers\ProductController.cs:line 163</p> <p><strong></strong></p> 2012-04-04T22:21:47-04:004916939http://forums.asp.net/p/1789334/4916939.aspx/1?Re+Url+Route+method+throwing+exceptionRe: Url.Route method throwing exception <p>Odd. Before making the call, check if the Url property is null. Based off the stacktrace, it is dying in the Url property getter.</p> 2012-04-05T01:45:05-04:004917031http://forums.asp.net/p/1789334/4917031.aspx/1?Re+Url+Route+method+throwing+exceptionRe: Url.Route method throwing exception <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Noel12345</h4> <span class="kwd">string</span><span class="pln"> uri </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Url</span><span class="pun">.</span><span class="typ">Route</span><span class="pun">(</span><span class="str">&quot;http://whatever/&quot;</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> id </span><span class="pun">=</span><span class="pln"> contact</span><span class="pun">.</span><span class="typ">Id</span><span class="pln"> </span><span class="pun">});</span><span class="pln"> </span></blockquote> <p></p> <p>What are doing, the first parameter is the <strong>routename</strong>(see routes in global.axax file for finding <strong>routename</strong>).</p> <p></p> 2012-04-05T03:48:01-04:004918640http://forums.asp.net/p/1789334/4918640.aspx/1?Re+Url+Route+method+throwing+exceptionRe: Url.Route method throwing exception <p>I see from examples this is set to null e.g.</p> <p><span class="kwd">string</span><span class="pln">&nbsp;uri&nbsp;</span><span class="pun">=</span><span class="pln">&nbsp;</span><span class="typ">Url</span><span class="pun">.</span><span class="typ">Route</span><span class="pun">(</span><span class="str">null</span><span class="pun">,</span><span class="pln">&nbsp;</span><span class="kwd">new</span><span class="pln">&nbsp;</span><span class="pun">{</span><span class="pln">&nbsp;id&nbsp;</span><span class="pun">=</span><span class="pln">&nbsp;contact</span><span class="pun">.</span><span class="typ">Id</span><span class="pln">&nbsp;</span><span class="pun">});</span></p> <p>And this work. Dont know how, if anyone can explain?</p> <p>Also how will this work for unit testing the controller. Currently blowing up?</p> 2012-04-05T20:23:06-04:004918673http://forums.asp.net/p/1789334/4918673.aspx/1?Re+Url+Route+method+throwing+exceptionRe: Url.Route method throwing exception <p>Is this blowing up in your unit tests or in your actual app ? </p> <p></p> 2012-04-05T20:58:54-04:004918679http://forums.asp.net/p/1789334/4918679.aspx/1?Re+Url+Route+method+throwing+exceptionRe: Url.Route method throwing exception <p>Works in application. Blows up when running unit test -&nbsp;Object reference not set to an instance of an object.</p> 2012-04-05T21:04:37-04:004918683http://forums.asp.net/p/1789334/4918683.aspx/1?Re+Url+Route+method+throwing+exceptionRe: Url.Route method throwing exception <p>I think you are not setting up the controller context property. Can you share your test ?</p> 2012-04-05T21:07:53-04:004918695http://forums.asp.net/p/1789334/4918695.aspx/1?Re+Url+Route+method+throwing+exceptionRe: Url.Route method throwing exception <p>Here it is</p> <pre class="prettyprint">[Test] public void T1() { ContactManager.Controllers.ContactsController contactsController = new ContactsController(); var contact = new Contact(); contact.Id = 1; contact.Name = &quot;N1&quot;; contact.Phone = &quot;123&quot;; contact.Email = &quot;dd@f&quot;; var result = contactsController.PostContact(contact); }</pre> <p><br> <br> </p> 2012-04-05T21:20:25-04:004918707http://forums.asp.net/p/1789334/4918707.aspx/1?Re+Url+Route+method+throwing+exceptionRe: Url.Route method throwing exception <p>You haven't set the controller context on your controller and hence the NullReferenceException. webapi initializes the ControllerContext property on your controller with the right value (request, controllerdescriptor and HttpConfiguration). If you are trying to test that then you need to set the context yourself.</p> <p>may be something like</p> <pre class="prettyprint">contactsController.ControllerContext = new HttpControllerContext(new HttpConfiguration(), new HttpRouteData(new HttpRoute(&quot;DummyRoute&quot;)), new HttpRequestMessage(&quot;http://localhost/api/contacts/1&quot;));</pre> <p><br> <br> </p> <p></p> 2012-04-05T21:39:30-04:004918722http://forums.asp.net/p/1789334/4918722.aspx/1?Re+Url+Route+method+throwing+exceptionRe: Url.Route method throwing exception <p>raghuramn,</p> <p>I have add that &nbsp;as follows:</p> <pre class="prettyprint">var _controllerContext = new HttpControllerContext(new HttpConfiguration(), new HttpRouteData(new HttpRoute(&quot;DummyRoute&quot;)), new HttpRequestMessage(HttpMethod.Post, &quot;http://localhost/api/contacts/1&quot;)); contactsController.ControllerContext = _controllerContext;</pre> <p></p> <p>And am now getting another exception from the same line -&nbsp;</p> <p>Value cannot be null.<br> Parameter name: name</p> <p>Any ideas?</p> 2012-04-05T22:01:08-04:004918749http://forums.asp.net/p/1789334/4918749.aspx/1?Re+Url+Route+method+throwing+exceptionRe: Url.Route method throwing exception <p>You haven't setup your configuration. You need to setup the configuration object to mirror what you are doing in your app. something like</p> <pre class="prettyprint">HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute(&quot;Default&quot;, &quot;api/{controller}/{action}&quot;); var _controllerContext = new HttpControllerContext(config, new HttpRouteData(new HttpRoute(&quot;Default&quot;)), new HttpRequestMessage(HttpMethod.Post, &quot;http://localhost/api/contacts/1&quot;));</pre> <p></p> <p>Also, the first parameter for the for Url.Route is the name of the route not a uri.</p> <p><br> <br> </p> 2012-04-05T23:19:11-04:004918850http://forums.asp.net/p/1789334/4918850.aspx/1?Re+Url+Route+method+throwing+exceptionRe: Url.Route method throwing exception <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Noel12345</h4> And this work. Dont know how, if anyone can explain?</blockquote> <p></p> <p>Passing <em>name</em> paramter with null or empty in Url.Route method causes the same effect as you call this overload of <a href="http://msdn.microsoft.com/en-us/library/cc680260.aspx">RouteCollection.GetVirtualPath</a>(which internally does not care name paramter if it is null or empty, and uses another overload <a href="http://msdn.microsoft.com/en-us/library/cc679806.aspx">RouteCollection.GetVirtualPath</a>).</p> <p>Note: Web host abstract the usage of RouteCollection class using HttpRouteCollection class. Although, the self host does not use RouteCollection class but the behaviour is same(AFAIK).&nbsp; See this for <a href="http://forums.asp.net/t/1777747.aspx/1?System&#43;Web&#43;Http&#43;Routing&#43;IHttpRouteConstraint&#43;vs&#43;System&#43;Web&#43;Routing&#43;IRouteConstraint"> detail</a>.</p> <p></p> 2012-04-06T03:45:26-04:004919535http://forums.asp.net/p/1789334/4919535.aspx/1?Re+Url+Route+method+throwing+exceptionRe: Url.Route method throwing exception <p>I have done the following:</p> <pre class="prettyprint">var _config = new HttpConfiguration(); _config.Routes.MapHttpRoute(&quot;DefaultApi&quot;, &quot;api/{controller}/{id}&quot;); var _controllerContext = new HttpControllerContext(_config, new HttpRouteData(new HttpRoute(&quot;DefaultApi&quot;)), new HttpRequestMessage(HttpMethod.Post, &quot;http://localhost/api/contacts&quot;)); contactsController.ControllerContext = _controllerContext;</pre> <p><br />Which I believe complies with my application set up:</p> <pre class="prettyprint">routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } );</pre> <p>However I am still getting the same error:</p> <p>Value cannot be null.<br> Parameter name: name</p> <p>What am I doing wrong? I have tried to find examples of testing controllers on web but cant find any</p> 2012-04-06T11:40:10-04:004919572http://forums.asp.net/p/1789334/4919572.aspx/1?Re+Url+Route+method+throwing+exceptionRe: Url.Route method throwing exception <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Noel12345</h4> <p></p> &lt;div class=&quot;comment-left-col&quot;&gt; <p class="post-points"></p> <p class="post-count"><a href="/searchresults/author/Noel12345">8 Posts</a></p> &lt;input id=&quot;quote-createdby&quot; value=&quot;Noel12345&quot; type=&quot;hidden&quot; /&gt;&lt;/div&gt; &lt;div class=&quot;comment-right-col&quot;&gt; <h3>Re: Url.Route method throwing exception</h3> <p class="date">4/6/2012 11:40:10 AM</p> &lt;div id=&quot;reference-post-content&quot; class=&quot;reply-comment&quot;&gt; <p>I have done the following:</p> <pre class="prettyprint">var _config = new HttpConfiguration(); _config.Routes.MapHttpRoute(&quot;DefaultApi&quot;, &quot;api/{controller}/{id}&quot;); var _controllerContext = new HttpControllerContext(_config, new HttpRouteData(new HttpRoute(&quot;DefaultApi&quot;)), new HttpRequestMessage(HttpMethod.Post, &quot;http://localhost/api/contacts&quot;)); contactsController.ControllerContext = _controllerContext;</pre> <p><br />Which I believe complies with my application set up:</p> <pre class="prettyprint"><span class="pln">routes</span><span class="pun">.</span><span class="typ">MapHttpRoute</span><span class="pun">(</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; &nbsp; name</span><span class="pun">:</span><span class="pln"> </span><span class="str">"DefaultApi"</span><span class="pun">,</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; &nbsp; routeTemplate</span><span class="pun">:</span><span class="pln"> </span><span class="str">"api/{controller}/{id}"</span><span class="pun">,</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; &nbsp; defaults</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> id </span><span class="pun">=</span><span class="pln"> </span><span class="typ">RouteParameter</span><span class="pun">.</span><span class="typ">Optional</span><span class="pln"> </span><span class="pun">}</span><span class="pln"><br /></span><span class="pun">);</span></pre> <p>However I am still getting the same error:</p> <p>Value cannot be null.<br /> Parameter name: name</p> &lt;/div&gt; &lt;/div&gt; <p></blockquote></p> <p>This may be bug in self host or null value in name paramter is not allowed in self host case. See the diffrence(from source),</p> <p>web host case,</p> <pre class="prettyprint"> if (string.IsNullOrEmpty(name)) { return this.GetVirtualPath(requestContext, values); } </pre> <p></p> <p>and self host case,</p> <pre class="prettyprint"> if (name == null) { throw Error...... }</pre> <p></p> <p></p> </blockquote> 2012-04-06T12:10:27-04:004919665http://forums.asp.net/p/1789334/4919665.aspx/1?Re+Url+Route+method+throwing+exceptionRe: Url.Route method throwing exception <p>imran_ku07,</p> <p>I can see where you are getting the if(name == null) .. -&nbsp;&nbsp;System.Web.Http.HttpRouteCollection.GetVirtualPath</p> <p>I dont understand where the&nbsp;<span class="kwd">if</span><span class="pln"> </span> <span class="pun">(</span><span class="kwd">string</span><span class="pun">.</span><span class="typ">IsNullOrEmpty</span><span class="pun">(</span><span class="pln">name</span><span class="pun">)) ... is getting called. Can you explain please?</span></p> 2012-04-06T13:12:13-04:004919683http://forums.asp.net/p/1789334/4919683.aspx/1?Re+Url+Route+method+throwing+exceptionRe: Url.Route method throwing exception <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Noel12345</h4> I dont understand where the&nbsp;<span class="kwd">if</span><span class="pln"> </span> <span class="pun">(</span><span class="kwd">string</span><span class="pun">.</span><span class="typ">IsNullOrEmpty</span><span class="pun">(</span><span class="pln">name</span><span class="pun">)) ... is getting called. Can you explain please?</span></blockquote> <p></p> <p>I think you have not read <a href="http://forums.asp.net/t/1777747.aspx/1?System&#43;Web&#43;Http&#43;Routing&#43;IHttpRouteConstraint&#43;vs&#43;System&#43;Web&#43;Routing&#43;IRouteConstraint"> this</a>. It is in System.Web.Routing.RouteCollection.</p> <p></p> 2012-04-06T13:24:22-04:00