Search code examples
asp.netasp.net-mvcrouteattribute

Route to action can not be found with route attribute in asp.net mvc


I use asp.net mvc with route attributing and I get this error:

System.Web.HttpException (0x80004005): No matching action was found on controller 'Test.RequestController'. This can happen when a controller uses RouteAttribute for routing, but no action on that controller matches the request.


public class TestController : Controller
    {      


        [Route("~/api/test")]
        [HttpPost]       
        public async Task<ActionResult> Post(TestRequest r)
        { 
            return new HttpStatusCodeResult(HttpStatusCode.Created);
        }
    }

Why is my route with localhost:5555/api/test with POST not found?

That is my default route setup:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapMvcAttributeRoutes();
        }

Solution

  • The RouteAttribute should not have a leading ~.