Re: Multiple actions were found that match the request Error WEB API MongoDB c#

312 views
Skip to first unread message

craiggwilson

unread,
Jan 12, 2013, 1:08:44 PM1/12/13
to mongod...@googlegroups.com
You have two methods with the same name only differing in case (GetDocsByID and getDocsByID) and (getAllDocs and GetAllDocs).  Depending on your host, it may consider urls to be case insensitive as well.  My assumption is that WebApi considers them to be case insensitive.  Remove one of the methods from each group.

On Friday, January 11, 2013 12:27:46 AM UTC-6, Michael Barnes wrote:
Im fairly new to ASP.NET MCV 4 as well as Mongo DB and trying to build web API.
I thought I had finally got it right but when I start the app and enter: `http://localhost:50491/api/document` into my browser I get this error message

       Multiple actions were found that match the request:
        System.Linq.IQueryable`1[Acord_Rest_API.Models.Document] GetAll() on type Acord_Rest_API.Controllers.DocumentController
        Acord_Rest_API.Models.Document Get(MongoDB.Bson.ObjectId) on type Acord_Rest_API.Controllers.DocumentController

Here is my code

This is the Document Class

    public class Document
    {
        [BsonId]
        public string ID { get; set; }
    }

This is where the Connection to the DB is made:
  
    public class MongoConnectionHelper<T> where T: class
    {
        public MongoCollection<T> collection { get; private set; }

        public MongoConnectionHelper()
        {
            string connectionString = "mongodb://127.0.0.1";
            var server = MongoServer.Create(connectionString);

            if (server.State == MongoServerState.Disconnected)
            {
                server.Connect();
            }

            var conn = server.GetDatabase("Acord");

            collection = conn.GetCollection<T>("Mappings");
        }
       
    }
Here is the ApiController Class:

    public class DocumentController : ApiController
    {
        public readonly IDocument docs;

        public DocumentController()
        {
            docs = new DocumentService();
        }

        public IQueryable<Document> GetAll()
        {
            return docs.GetAllDocs().AsQueryable();
        }

        public Document GetByDocumentID(string ID)
        {
            var byID = docs.GetDocsByID(ID);
            return byID;
        }
    }
       
Here is my Document Interface

    interface IDocument
    {
        IEnumerable<Document> GetAllDocs();
        Document GetDocsByID(string ID);
    }

Here is my Document Service class

    public class DocumentService : IDocument
    {
        private readonly MongoConnectionHelper<Document> doc;

        public DocumentService()
        {
            doc = new MongoConnectionHelper<Document>();
        }

        public IEnumerable<Document> getAllDocs()
        {
            var alldocs = (doc.collection.FindAll());
            return alldocs;
        }

        public Document getDocByID(string ID)
        {
            IMongoQuery query = Query.EQ("_id", ID);
            var result = doc.collection.Find(query).FirstOrDefault();
            return result;
        }

        public IList<Document> GetAllDocs()
        {
            throw new NotImplementedException();
        }

        public Document GetDocsByID(string ID)
        {
            throw new NotImplementedException();
        }
    }

Here is my global.asax:

    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }

Here is the route, I have modified it and added in `{action}`:

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

What should I do? I have tried so many tutorials!
I have added all the code because I really want a solution!
All I want is to return all the Documents and to return one by its _id!
Please  HELP!!!
Reply all
Reply to author
Forward
0 new messages