I currently have two controllers. One is MVC and the other is a WebApi controller.
I've published these two with ease on the IIS server with the following settings:
WebApiConfig:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
RouteConfig:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "MVCControllerName", action = "MVCControllerName", id = UrlParameter.Optional }
);
Now I've added another WebApi controller, but when published, the webserver says:
No HTTP resource was found that matches the request URI
http://example.com/folder/api/Other?id=45&text=bla
Implementation of the OtherController:
public void Get(int id, string text)
{
// something simply gets commited to the DB
}
Also, something weird is happening when publishing... I've deleted the first WebApi controller from the solution, but for some reason when publishing I can STILL call that WebApi through HTTP GET request! Even though it's gone!
OTOH, it works fine in the debug mode..
What's going on here?
EDIT: It seems the problem is not in my code, but in VS' publishing feature. It has always worked until now. I had to manually copy the files to the server, which isn't the point of so-called "one-click publishing". Other similar SO questions seem to indicate it is a VS bug that is still not fixed in SP2.
Related
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.
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.
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.
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.
This question may seems duplicate but this is slightly different.
In all other question in SO I had noticed that they have multiple routes registered. but in my case I have just one route.
I am creating asp.net webapi (framework 4.5) and have just one route in RegisterRoutes() method -
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "DefaultApi",
url: "rest/{controller}/{id}",
defaults: new { id = UrlParameter.Optional }
);
}
Then why is it throwing error?
A route named 'DefaultApi' is already in the route collection. Route names must be unique. Parameter name: name
I had a similar issue with adding a route DefaultApi. Though the exact 'additional details' message in my ArgumentException stack trace was:
A route named 'MS_attributerouteWebApi' is already in the route collection.
Route names must be unique.
I ofcourse made sure I was adding the DefaultApi route only once, but in the end noticed that in Global.asax's Application_Start method I was calling the WebApiConfig.Register(..) twice, though in the following - not immediately obvious - way:
WebApiConfig.Register(GlobalConfiguration.Configuration);
GlobalConfiguration.Configure(WebApiConfig.Register);
Another serious case of 'copypasterites'! I simply removed the WebApiConfig.Register(..) line and that fixed my issue.
(I am using WEB API 2.0/.NET 5)
CAUSE: renaming namespaces without removing associated bin and output files.
SOLUTION: manually delete the bin and obj folders from the output directory. (cleaning the solution is not enough, some problematic residual files remain causing this problem.)
... that was my experience anyway.
Fine, I resolved it based on the reply by user3038092. Instead of adding it in the route collection, I added it in HttpConfiguration
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "rest/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
And it worked.
If you are using MVC4 or MVC5 application.
Then put your Route Configuration in
WebApiConfig.cs and
also check route name should be unique in both files i.e RouteConfig.cs and WebApiConfig.cs
I see that you call the controller but you do not give the controller name like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "DefaultApi",
url: "rest/{controller}/{id}",
defaults: new { controller="Home",action="Index",id = UrlParameter.Optional }
);
}
that means you regist HomeContrller for the Index view,