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.
may be something like
contactsController.ControllerContext = new HttpControllerContext(new HttpConfiguration(), new HttpRouteData(new HttpRoute("DummyRoute")), new HttpRequestMessage("http://localhost/api/contacts/1"));
var _controllerContext = new HttpControllerContext(new HttpConfiguration(),
new HttpRouteData(new HttpRoute("DummyRoute")), new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/contacts/1"));
contactsController.ControllerContext = _controllerContext;
And am now getting another exception from the same line -
You haven't setup your configuration. You need to setup the configuration object to mirror what you are doing in your app. something like
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute("Default", "api/{controller}/{action}");
var _controllerContext = new HttpControllerContext(config,
new HttpRouteData(new HttpRoute("Default")), new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/contacts/1"));
Also, the first parameter for the for Url.Route is the name of the route not a uri.
And this work. Dont know how, if anyone can explain?
Passing name paramter with null or empty in Url.Route method causes the same effect as you call this overload of
RouteCollection.GetVirtualPath(which internally does not care name paramter if it is null or empty, and uses another overload
RouteCollection.GetVirtualPath).
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). See this for
detail.
"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
var _config = new HttpConfiguration();
_config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
var _controllerContext = new HttpControllerContext(_config,
new HttpRouteData(new HttpRoute("DefaultApi")),
new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/contacts"));
contactsController.ControllerContext = _controllerContext;
Which I believe complies with my application set up:
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
However I am still getting the same error:
Value cannot be null.
Parameter name: name
What am I doing wrong? I have tried to find examples of testing controllers on web but cant find any
var _config = new HttpConfiguration();
_config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
var _controllerContext = new HttpControllerContext(_config,
new HttpRouteData(new HttpRoute("DefaultApi")),
new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/contacts"));
contactsController.ControllerContext = _controllerContext;
Which I believe complies with my application set up:
routes.MapHttpRoute( name:"DefaultApi", routeTemplate:"api/{controller}/{id}", defaults:new{ id =RouteParameter.Optional} );
However I am still getting the same error:
Value cannot be null. Parameter name: name
</div>
</div>
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),
web host case,
if (string.IsNullOrEmpty(name))
{
return this.GetVirtualPath(requestContext, values);
}
and self host case,
if (name == null)
{
throw Error......
}
"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
I dont understand where the if(string.IsNullOrEmpty(name)) ... is getting called. Can you explain please?
I think you have not read
this. It is in System.Web.Routing.RouteCollection.
"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
raghuramn
Member
248 Points
64 Posts
Microsoft
Re: Url.Route method throwing exception
Apr 05, 2012 09:07 PM|LINK
I think you are not setting up the controller context property. Can you share your test ?
Noel12345
Member
1 Points
18 Posts
Re: Url.Route method throwing exception
Apr 05, 2012 09:20 PM|LINK
Here it is
[Test] public void T1() { ContactManager.Controllers.ContactsController contactsController = new ContactsController(); var contact = new Contact(); contact.Id = 1; contact.Name = "N1"; contact.Phone = "123"; contact.Email = "dd@f"; var result = contactsController.PostContact(contact); }raghuramn
Member
248 Points
64 Posts
Microsoft
Re: Url.Route method throwing exception
Apr 05, 2012 09:39 PM|LINK
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.
may be something like
contactsController.ControllerContext = new HttpControllerContext(new HttpConfiguration(), new HttpRouteData(new HttpRoute("DummyRoute")), new HttpRequestMessage("http://localhost/api/contacts/1"));Noel12345
Member
1 Points
18 Posts
Re: Url.Route method throwing exception
Apr 05, 2012 10:01 PM|LINK
raghuramn,
I have add that as follows:
var _controllerContext = new HttpControllerContext(new HttpConfiguration(), new HttpRouteData(new HttpRoute("DummyRoute")), new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/contacts/1")); contactsController.ControllerContext = _controllerContext;And am now getting another exception from the same line -
Value cannot be null.
Parameter name: name
Any ideas?
raghuramn
Member
248 Points
64 Posts
Microsoft
Re: Url.Route method throwing exception
Apr 05, 2012 11:19 PM|LINK
You haven't setup your configuration. You need to setup the configuration object to mirror what you are doing in your app. something like
HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute("Default", "api/{controller}/{action}"); var _controllerContext = new HttpControllerContext(config, new HttpRouteData(new HttpRoute("Default")), new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/contacts/1"));Also, the first parameter for the for Url.Route is the name of the route not a uri.
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Url.Route method throwing exception
Apr 06, 2012 03:45 AM|LINK
Passing name paramter with null or empty in Url.Route method causes the same effect as you call this overload of RouteCollection.GetVirtualPath(which internally does not care name paramter if it is null or empty, and uses another overload RouteCollection.GetVirtualPath).
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). See this for detail.
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Noel12345
Member
1 Points
18 Posts
Re: Url.Route method throwing exception
Apr 06, 2012 11:40 AM|LINK
I have done the following:
var _config = new HttpConfiguration(); _config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}"); var _controllerContext = new HttpControllerContext(_config, new HttpRouteData(new HttpRoute("DefaultApi")), new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/contacts")); contactsController.ControllerContext = _controllerContext;Which I believe complies with my application set up:
routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } );However I am still getting the same error:
Value cannot be null.
Parameter name: name
What am I doing wrong? I have tried to find examples of testing controllers on web but cant find any
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Url.Route method throwing exception
Apr 06, 2012 12:10 PM|LINK
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),
web host case,
if (string.IsNullOrEmpty(name)) { return this.GetVirtualPath(requestContext, values); }and self host case,
if (name == null) { throw Error...... }Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Noel12345
Member
1 Points
18 Posts
Re: Url.Route method throwing exception
Apr 06, 2012 01:12 PM|LINK
imran_ku07,
I can see where you are getting the if(name == null) .. - System.Web.Http.HttpRouteCollection.GetVirtualPath
I dont understand where the if (string.IsNullOrEmpty(name)) ... is getting called. Can you explain please?
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Url.Route method throwing exception
Apr 06, 2012 01:24 PM|LINK
I think you have not read this. It is in System.Web.Routing.RouteCollection.
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD