Web API Routing - multiple actions were found that match the request - c#

I got this Route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
And this Actions:
[System.Web.Http.HttpPost]
[System.Web.Http.ActionName("GetLoginSeed")]
public object GetLoginSeed()
[System.Web.Http.HttpPost]
[System.Web.Http.AllowAnonymous]
[System.Web.Http.ActionName("Authenticate")]
public object PerformLogin(JObject jr)
This is the Post Request:
http://localhost:61971/api/Login/GetLoginSeed
Why I always get an multiple actions were found that match the request error?

I got this Route:
What you have shown is a route for MVC controllers. I hope you realize that Web API controllers are an entirely different thing. They have their own routes defined in the ~/App_Start/WebApiConfig.cs.
So make sure tat you have included the {action} token in your Web API route definition (which I repeat once again has nothing to do with your MVC route definitions):
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}"
);

Related

Default controller giving me a "No HTTP resource was found that matches the request URI" -"No type was found that matches the controller named 'tab'

I'm trying to access an app on my localhost connected to IIS with the following endpoint I'm trying to hit https://api.url.com/api/tab. I have a TabController.cs in my Controllers folder. I also have a Views/Tab/Index.cshtml file and am wondering why I'm getting the following two errors:
<Error>
<Message>No HTTP resource was found that matches the request URI 'https://api.url.com/api/tab'.</Message>
<MessageDetail>No type was found that matches the controller named 'tab'.</MessageDetail>
</Error>
I have the same folder structure for a controller named Footer, and I'm able to access https://api.url.com/api/footer successfully. I've included my WebApiConfig.cs and RouteConfig.cs code below:
WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
I've a feeling it's something quite obvious I'm missing as I'm a bit new to .NET and the MVC structure. So any point in the right direction would be greatly appreciated.
You trying to access the MVC Controller (inherited from the System.Web.Mvc.Controller) as if it's the Web API Controller (inherited from the System.Web.Http.ApiController). As you know API controllers have their own routing configuration, which is completely separate from the rest of
the application. Therefore, to access an action method from the regular MVC controller don't use api prefix in the URL. Just enter https://api.url.com/tab to call the default action method in the tab controller.

MVC & WebAPI "Multiple types were found that match the controller named..."

I have a routing problem with MVC and WebAPI controllers.
The error is:
Multiple types were found that match the controller named 'Log'. This
can happen if the route that services this request
('api/{controller}/{action}/{id}') found multiple controllers defined
with the same name but differing namespaces, which is not supported.
The request for 'Log' has found the following matching controllers:
MyNamespace.Controllers.LogController
MyNamespace.Controllers.WebAPI.LogController
My routing is simple:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
I have tried adding an additional route for WebAPI like:
routes.MapHttpRoute(
name: "API",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Along with adding a namespace for the MVC route:
namespaces: new[] { "MyNamespace.Controllers" }
But I still receive the same error. I cannot see a way to specify the namespace for the WebAPI route.
There is a well-upvoted duplicate but that is specifically about MVC controllers. The answers talk about adding a namespace as above, which has not helped here. In my case the routing to the MVC controller works fine, and it is only the WebAPI controller where I have the problem.
The strange thing is, all my other WebAPI controllers have the same name as MVC controllers but this is the only one where I run into the error.
If you changed the name of the project, you must delete the files in the bin folder with the old project name.

Narrowing scope of C# MVC Routing to a namespace or partial group of controllers

I have two different categories of endpoints on a single C# site:
1) A normal MVC site serving up html
2) A JSON API
I'm looking for a way to route requests starting with api only to API controllers, and everything else through the normal MVC controllers.
The two groups of controllers are in different namespaces, but apparently when it's building these routes, it looks at all namespaces?
What I have below won't work because, essentially, the second group is too permissive and will route to API controllers, but I'm not sure what to do instead to scope or namespace these requests.
routes.MapRoute(
name: "Api",
url: "api/{controller}/{action}/{id}",
defaults: new { action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional}
);

Multiple controllers match the URL WebAPI

I have two web api methods with following route url's
[HTTPGET]
[Route("{Code}/{Id}")]
[HTTPGET]
[Route("{Code}/counter")]
Request /01/counter
{Id} is also a string parameter. Hence I am now getting error when calling Second api. "Multiple controllers action found for this Url" as webapi considers /01/counter valid for both routes.
I have seen few solutions with regex but can't find a working one yet. What is the good solution for this so that both Url's work as expected.
UPDATE:
I Found that the issue was occuring as the two methods were in different controllers hence webapi was having a problem in deciding which controller to choose. Once I moved them in the same controller, the problem is solved since , the route arguments are checked after controller is fixed.
If you are using WebAPI 2, you can use the RouteOrder property to define precedence when multiple actions match.
https://learn.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#route-order
[HttpGet]
[Route("{Code}/{Id}", RouteOrder = 2)]
[HttpGet]
[Route("{Code}/counter", RouteOrder = 1)]
If you are using MVC, you can use Order property:
https://learn.microsoft.com/en-us/previous-versions/aspnet/mt150670(v=vs.118)
[HttpGet]
[Route("{Code}/{Id}", Order = 2)]
[HttpGet]
[Route("{Code}/counter", Order = 1)]
There is no problem in your snippet code.
[HTTPGET]
[Route("{Code}/{Id}")]
[HTTPGET]
[Route("{Code}/counter")]
It seems that in your Web API routing configuration action is not specifying like this. It's by default setting provided by the template.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
In WebApi.config file use this code
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional, action = RouteParameter.Optional }
);
Hopefully it will solve your problem.

Web API 2 - restful service - URL encoded

I have created a RESTful service using web API 2. I have the following route to return information about a stock item :
http://localhost/api/stockitems/{stockCode}
i.e. http://localhost/api/stockitems/BOMTEST1
I have stock codes in my system that contain forward slashes i.e. CA/BASE/SNG/BEECH. Naturally i can't request the details using the standard convention due to the slashes.
http://localhost/api/stockitems/CA/BASE/SNG/BEECH
I have tried URL encoding but it's not hitting the controller
http://localhost/api/stockitems/CA%2FBASE%2FSNG%2FBEECH
I just keep getting a 404
How do i handle this in Web API?
You will need to change your WebApiConfig. If you don't use IDs in more than this place you can just add a wildcard ({*id}) in to that part of the config:
config.Routes.MapHttpRoute(
name: "Default",
routeTemplate: "api/{controller}/{*id}",
defaults: new { id = RouteParameter.Optional }
);
I'd recommend creating a specific route for this case tho (assuming it is the only scenario where you need to allow slashes):
config.Routes.MapHttpRoute(
name: "StockItems",
routeTemplate: "api/stockitems/{*id}",
defaults: new { controller = "StockItems", id = RouteParameter.Optional }
);
You won't need to url encode the URLs this way.

Categories

Resources