0

I have created a mvc project, I have multiple mvc controller in my project, that are working fine. I have added a web API controller Cities as well in project under Controller folder.

Now I am unable to call it, actually i am unable to fine the url for this controller. I have added static WebApiConfig class in my project and call it in Application_Start method.

//application start.
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            GlobalConfiguration.Configure(WebApiConfig.Register);
        }

//Register web api route
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }

Web api controller:

    public class CitiesController : ApiController
    {
        CityEntities _entity = new CityEntities();

        // GET: api/Cities
        [HttpGet]
        public IEnumerable<City> Get()
        {
            return _entity.Cities.ToList();
        }

        // GET: api/Cities/5
        public City Get(int id)
        {
            return _entity.Cities.Where(x=>x.city_id==id).FirstOrDefault();
        }
    }

the URL that i did try to call Cities controller are.

But not work.

enter image description here

3
  • 1
    Assuming the method name is Get(), you aren't supposed to append /Get to the Url. Commented Nov 8, 2017 at 13:37
  • the url is without the verb /api/Cities Commented Nov 8, 2017 at 13:42
  • Can you tell me url? Commented Nov 8, 2017 at 13:44

1 Answer 1

2

If you are not using attribute routing for that action, then action should map to default API route. Take a look at default route template for api routes:

api/{controller}/{id}

Things to consider:

  • This route has an api path segment. Thus your second url will not match default api route
  • This route does not have action parameter. That's because actions of ApiControllers are mapped by HTTP method of the request. You should not specify action name in request url. By convention, for HTTP GET requests name of action should begin with Get word.

So, your actions should be mapped to following HTTP GET requests:

GET http://localhost:56261/api/Cities
GET http://localhost:56261/api/Cities/5

Further reading: Routing in ASP.NET Web API

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

6 Comments

@UsfNoor then you should show defnintion of Cities controller and action which you are trying to execute. What is the controller name? Do you inherit from ApiController? Do you use routing attributes? What is the name of action? Also make sure port number is correct
i have added Cities controller code, please check
Port number is correct, because other mvc controllers are executing fine
@UsfNoor for your controller it should be GET requests to /api/cities/ and /api/cities/5. I wonder why your comments say api/apiCity and api/apiCity/5 urls?
@UsfNoor then you missed something important by replacing names for stackoverflow
|

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.