Reputation: 741
I have a Controller
called api
, the following
routes.MapRoute(
name: "api",
url: "api/",
defaults: new { controller = "api" }
);
inside the controller I have an action as follows
[Route("fleet/{id:guid}/selectedfleet")]
public ActionResult selectedfleet(Guid id)
{
return null;
}
the return null
is just to test this out, I'm trying to get a breakpoint on it
but i am getting url not found, when trying to access something like http://localhost:50593/api/fleet/2df3893d-c406-48fe-8443-1622ddc51af2/selectedfleet
Upvotes: 1
Views: 501
Reputation: 247018
First you need to make sure that if you want to use attribute routing in MVC, that it is enabled.
public class RouteConfig {
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes(); //<--IMPORTANT for attribute routing in MVC
// Convention-based routing.
//...other code removed for brevity
}
}
when trying to access something like http://localhost:50593/api/fleet/2df3893d-c406-48fe-8443-1622ddc51af2/selectedfleet
via attribute routing you need to make sure that the controller has the proper route template defined to match the request.
[RoutePrefix("api")]
public class ApiController : Controller {
//GET api/fleet/2df3893d-c406-48fe-8443-1622ddc51af2/selectedfleet
[HttpGet]
[Route("fleet/{id:guid}/selectedfleet")]
public ActionResult selectedfleet(Guid id) { ... }
}
Make sure that your controller and route does not conflict Web API routes if you have it included in the MVC project.
Upvotes: 1
Reputation: 218722
The routing pattern you defined in the Route
attribute does not include the term "api"
. So with your current route definition, the below request will work.
yourSiteBaseUrl/fleet/2df3893d-c406-48fe-8443-1622ddc51af2/selectedfleet
Or you can add the term api in the route definition.
public class apiController : Controller
{
[Route("api/fleet/{id:guid}/selectedfleet")]
public ActionResult selectedfleet(Guid id)
{
return Content(id.ToString());
}
}
OR using RoutePrefix
attribute on the controller level
[RoutePrefix("api")]
public class apiController : Controller
{
[Route("fleet/{id:guid}/selectedfleet")]
public ActionResult selectedfleet(Guid id)
{
return Content(id.ToString());
}
}
Now it will work for the below url
yourSiteBaseUrl/api/fleet/0a5bb04d-4247-4cf6-8f96-2ce49325b5a7/selectedfleet
Upvotes: 1