Reputation: 391
I'm facing the below error while ruing my asp.net website project.
Error: No type was found that matches the controller named 'XXXX'.
Route Config:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
// AuthConfig.RegisterOpenAuth();
RouteConfig.RegisterRoutes(RouteTable.Routes);
System.Web.Http.GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = System.Web.Http.RouteParameter.Optional });
}
WebApi Controller:
public class SampleWebController : ApiController
{
public object SampleAction(Dictionary<string, string> jsonResult)
{
}
}
URL: ServiceUrl="../api/SampleWeb"
Please any one provide an idea to over come this error.
Also let me know if i'm doing any think wrong here.
Thanks in advance.
Upvotes: 2
Views: 2549
Reputation: 7348
If you have multiple POST actions in the same controller you should make the Route Config like this:
System.Web.Http.GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { id = System.Web.Http.RouteParameter.Optional });
Then in your controller you can have multiple GET and POST methods
public class TestController : ApiController
{
[ActionName("PostMe")]
public object PostMe()
{
}
[ActionName("PostMeTwo")]
public object PostMeTwo()
{
}
[HttpGet]
public object TestGet()
{
}
}
Then you can generate a POST request to the action either using Ajax
or PostMan
like this:
localhost:XXXX/Test/PostMe
Where Test
is name of controller and PostMe
Name of action - Both Required
localhost:XXXX/Test/PostMeTwo[POST]
localhost:XXXX/Test/TestGet [GET]
Upvotes: 1
Reputation: 4051
You have 2 options.
First option, add an action name to your controller
public class SampleWebController : ApiController
{
[ActionName("SampleAction")]
public object SampleAction(Dictionary<string, string> jsonResult)
{
}
}
And you would call it like "../api/SampleWeb/SampleAction"
This way you don't need to change your route config
Second option, change your route config to
routeTemplate: "api/{controller}/{id}",
and your method to
public class SampleWebController : ApiController
{
[HttpGet]
public object GetSampleAction(Dictionary<string, string> jsonResult)
{
}
}
You can then call '../api/SampleWeb' if you are making a get request.
NOTE: If you are going to have multiple gets, posts, puts, etc in the same controller, go with the first option. If you only plan on a single get, post, put, etc for each controller then option 2 is much cleaner.
EDIT: To test option 1, change your method to
public class SampleWebController : ApiController
{
[ActionName("SampleAction")]
public object SampleAction(int id)
{
return id;
}
}
And call you api like '.../api/SampleWeb/SampleAction/10'. If this works it means the data you are passing to SampleAction can't be converted to Dictionary and thats your issue, not webapi or your routing. Make sure your route config is still
routeTemplate: "api/{controller}/{action}/{id}",
Upvotes: 0