0

I have a Web API 2 that I am consuming using jQuery Ajax and looking through the documentation you can now include a route inside the Controller itself so you could include additional parameters inside the GET request for example.

I have tried adding this for example:

   [Route("api/formHTML/{id}/{code}/{value}")]
        public IEnumerable<string> Get(int id, int code, int value)
        {
            return new string[] {"<html><head></head><body>Test</body></html>"};
        }

Which will basically return some HTML code.

I keep getting 404 not found when using a GET on api/formHTML/1/5/2

I still have this in my Global.asax which contains the default route mapping.

protected void Application_Start(object sender, EventArgs e)
    {
         GlobalConfiguration.Configuration.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}"
        );
    }

Does anyone know why I would be getting a 404?

I know I could get the values by sending them as data object in the Get Request and using formBody in the Web API but I ideally wanted to use the above convention.

Thanks

1 Answer 1

1

I have resolved this by reading the documentation again, I moved the default mapping to WebApiConfig.cs and then called this method from the Global.asax:

  GlobalConfiguration.Configure(WebApiConfig.Register);

WebApiConfig.cs contains:

  public class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();
            config.Routes.MapHttpRoute(
              name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = System.Web.Http.RouteParameter.Optional }
           );
        }
    }

My issue was resolved!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.