Reputation: 764
I can't find the controller route in my new MVC 5 app. I've been following along with a few tutorials and everything appears to be written properly, but when browsing the address in the browser I always get:
No HTTP resource was found that matches the request URI "//localhost:50473/api/tile"
/Controller/ProductController.cs
using System;
using System.IO;
using System.Web;
using System.Net;
using coal.Models;
using System.Linq;
using System.Text;
using System.Net.Http;
using System.Web.Http;
using System.Collections.Generic;
namespace coal.Controllers
{
public class ProductController : ApiController
{
public void GetAllProducts() { }
[HttpGet]
public string Tiles(string urls) { }
}
}
/App_Start/WebApiConfig.cs
namespace coal
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "api",
routeTemplate: "api/{controller}/{id}",
defaults: new { controller = "coal", action = "Index", id = "" }
);
}
}
}
I know I'm missing something, just can't figure out what it is. Any insight would help.
Thanks
Upvotes: 2
Views: 17298
Reputation: 8475
The name of your method is Tiles
, but you're trying to hit tile
.
public string Tiles(string urls) { }
Also, your route is defined as api/{controller}/{action}
, and your controller name is ProductController.
The url you should use is: api/product/tiles
, unless you create a route that maps the Product/tiles
action to the api/tiles
route.
and your route should look something like:
config.Routes.MapHttpRoute(
name: "Default",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Upvotes: 6
Reputation: 6975
Your route mapping needs to allow mvc to work out which controller and action method you're looking for. This can be done in two ways, either you can have a {controller}
and {action}
in your route string, or you can specify a controller
and action
in the defaults
.
So for example you could set up a route like this:
config.Routes.MapHttpRoute(
name: "Default",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Then the URL to your method would be http://localhost:50473/api/Product/Tiles
, matching the controller name "Product" and the action method name "tiles".
Alternatively, maybe you want the URL you specified to match, in which case you could do the following:
config.Routes.MapHttpRoute(
name: "Tiles",
routeTemplate: "api/tile/",
defaults: new { controller = "Product", action = "Tiles" }
);
Upvotes: 0