Web api routing with entity framework

asp.net-web-api2 c# entity-framework-6 routing

Question

I'm using the Web Api 2 framework in order to provide HTTP services to an angular website. I followed the official guide and everything seems so work, except that when I call GET, POST... methods from POSTMAN the result is always the same: The resource cannot be found.

Here are the routing configurations:

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

The code of the controller (my controller is called EndpointModelsController) is the auto-generated code from visual studio. I searched for a solution and I tried with routing Attributes in this guide but the result was the same. In my case the web service is running on port 54800 and I'm using IISEXPRESS: an URL example could be this one http://localhost:54800/api/EndpointModels for a GET request without any parameter.

Is the url wrong or do you think the problem is in the code? what could the causes be?

Thank you.

[edit]

Here is the code of the controller: I've only added [HttpGet] attribute.

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using CodeProbe.WebApp.Models;

namespace CodeProbe.WebApp.Controllers
{
    public class EndpointModelsController : ApiController
    {
        private EndpointContext db = new EndpointContext();

        // GET: api/EndpointModels
        [HttpGet]
        public IQueryable<EndpointModels> GetEndPoint()
        {
            return db.EndPoint;
        }

        // GET: api/EndpointModels/5
        [ResponseType(typeof(EndpointModels))]
        [HttpGet]
        public async Task<IHttpActionResult> GetEndpointModels(string id)
        {
            EndpointModels endpointModels = await db.EndPoint.FindAsync(id);
            if (endpointModels == null)
            {
                return NotFound();
            }

            return Ok(endpointModels);
        }

        // PUT: api/EndpointModels/5
        [ResponseType(typeof(void))]
        public async Task<IHttpActionResult> PutEndpointModels(string id, EndpointModels endpointModels)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != endpointModels.ServiceName)
            {
                return BadRequest();
            }

            db.Entry(endpointModels).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EndpointModelsExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/EndpointModels
        [ResponseType(typeof(EndpointModels))]
        public async Task<IHttpActionResult> PostEndpointModels(EndpointModels endpointModels)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.EndPoint.Add(endpointModels);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (EndpointModelsExists(endpointModels.ServiceName))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = endpointModels.ServiceName }, endpointModels);
        }

        // DELETE: api/EndpointModels/5
        [ResponseType(typeof(EndpointModels))]
        public async Task<IHttpActionResult> DeleteEndpointModels(string id)
        {
            EndpointModels endpointModels = await db.EndPoint.FindAsync(id);
            if (endpointModels == null)
            {
                return NotFound();
            }

            db.EndPoint.Remove(endpointModels);
            await db.SaveChangesAsync();

            return Ok(endpointModels);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool EndpointModelsExists(string id)
        {
            return db.EndPoint.Count(e => e.ServiceName == id) > 0;
        }
    }
}
1
0
6/26/2017 11:27:51 AM

Popular Answer

Try This in Configuration

        //cross Domain
        var cors = new EnableCorsAttribute("*", "*", "*");
        config.EnableCors(cors);

This nuget Package Install-Package Microsoft.AspNetCore.Cors

1
6/26/2017 11:53:01 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow