I have the following code that is throwing an exception(Object reference not set to an instance of an object.) on the line below.
string uri = Url.Route("http://whatever/", new { id = contact.Id });
Anyone know why this does not work? Thanks
public HttpResponseMessage<Product> Post(Product product)
{
_productsRepository.Add(product);
var response = new HttpResponseMessage<Product>(product, HttpStatusCode.Created);
string uri = Url.Route("http://whatever/", new { id = contact.Id });
response.Headers.Location = new Uri(Request.RequestUri, uri);
return response;
}
string uri =Url.Route("http://whatever/",new{ id
= contact.Id});
What are doing, the first parameter is the routename(see routes in global.axax file for finding
routename).
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Noel12345
Member
1 Points
18 Posts
Url.Route method throwing exception
Apr 04, 2012 09:48 PM|LINK
Hi,
I have the following code that is throwing an exception(Object reference not set to an instance of an object.) on the line below.
string uri = Url.Route("http://whatever/", new { id = contact.Id });Anyone know why this does not work?
Thanks
public HttpResponseMessage<Product> Post(Product product) { _productsRepository.Add(product); var response = new HttpResponseMessage<Product>(product, HttpStatusCode.Created); string uri = Url.Route("http://whatever/", new { id = contact.Id }); response.Headers.Location = new Uri(Request.RequestUri, uri); return response; }davebettin
Member
313 Points
94 Posts
Re: Url.Route method throwing exception
Apr 04, 2012 09:52 PM|LINK
Is contact a private member that could potentially be null? Or does the stacktrace show the exception being thrown in the Route method?
@dbettin
Noel12345
Member
1 Points
18 Posts
Re: Url.Route method throwing exception
Apr 04, 2012 10:10 PM|LINK
StackTrace is what I have giving already - there is no inner exception. Contact is public is no problem there.
davebettin
Member
313 Points
94 Posts
Re: Url.Route method throwing exception
Apr 04, 2012 10:14 PM|LINK
I don't see the exception stacktrace. All I see is the exception message in your post.
@dbettin
Noel12345
Member
1 Points
18 Posts
Re: Url.Route method throwing exception
Apr 04, 2012 10:21 PM|LINK
Sorry,StackTrace:
at System.Web.Http.ApiController.get_Url()
at TestApp.Controllers.ProductController.Post(RateSet newRateSet) in D:\TestApp\Controllers\ProductController.cs:line 163
davebettin
Member
313 Points
94 Posts
Re: Url.Route method throwing exception
Apr 05, 2012 01:45 AM|LINK
Odd. Before making the call, check if the Url property is null. Based off the stacktrace, it is dying in the Url property getter.
@dbettin
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Url.Route method throwing exception
Apr 05, 2012 03:48 AM|LINK
What are doing, the first parameter is the routename(see routes in global.axax file for finding routename).
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Noel12345
Member
1 Points
18 Posts
Re: Url.Route method throwing exception
Apr 05, 2012 08:23 PM|LINK
I see from examples this is set to null e.g.
string uri = Url.Route(null, new { id = contact.Id });
And this work. Dont know how, if anyone can explain?
Also how will this work for unit testing the controller. Currently blowing up?
raghuramn
Member
248 Points
64 Posts
Microsoft
Re: Url.Route method throwing exception
Apr 05, 2012 08:58 PM|LINK
Is this blowing up in your unit tests or in your actual app ?
Noel12345
Member
1 Points
18 Posts
Re: Url.Route method throwing exception
Apr 05, 2012 09:04 PM|LINK
Works in application. Blows up when running unit test - Object reference not set to an instance of an object.