How to (automatically) generate a webapi controller for MVC + Entity or generally request all types

As a newbie to MVC4 + Entity + WebAPI, I was frustrated by the fact that I just had GET, POST controller handlers, and user mappings with data models.

I think I am asking if there is a generation tool, for example, to make my controller class from your data class so that I can just execute simple GET commands directly from the data?

What is the approach for creating a generic RESTful API, so commands can be made as such

GET api / 1.0 / {genericdatatype} / {id}

where can a common data type be any model and specific controllers? Let's say I don't need PUT (handled through an MVC application), so I really don't need POST checking, etc.

+5
source share
3 answers

There is a tool / package called MVC Scaffolding that will create your controllers based on your models.

http://mvcscaffolding.codeplex.com/

http://blog.stevensanderson.com/2011/01/13/scaffold-your-aspnet-mvc-3-project-with-the-mvcscaffolding-package/

As for the general part, this is a very long and complex process that will take a lot of time. I would really like to see this if someone has a good answer.

EDIT: I spent some extra time looking for a generic work. It seems that someone had a similar thought and asked almost the same question: Generic Web Api controller to support any model .

. ? , ? -, , - . , , , API. , , , , API? .

+3

, , , , .

api- CRUD MVC 5:

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.Web.Http;
using My.Models;
using System.Data.Entity.Validation;

namespace My.Controllers.Api
{
  public abstract class CrudController<TEntity>
    : ApiController where TEntity : class
  {
    private readonly MyContext _db = new MyContext();
    private readonly DbSet<TEntity> _dbSet;

    protected CrudController(Func<MyContext, DbSet<TEntity>> dbSet)
    {
      _db = new EtlContext();
      _dbSet = dbSet(_db);
    }

    public IEnumerable<TEntity> Get()
    {
      return _dbSet.AsEnumerable();
    }

    public HttpResponseMessage Post(TEntity entity)
    {
      try
      {
        if (!ModelState.IsValid)
          return Request.CreateResponse(HttpStatusCode.BadRequest);

        _db.Entry(entity).State = EntityState.Added;

        _db.SaveChanges();

        return Request.CreateResponse(HttpStatusCode.Created);
      }
      catch (DbEntityValidationException)
      {
        return Request.CreateResponse(HttpStatusCode.BadRequest);
      }
      catch (DbUpdateException)
      {
        return Request.CreateResponse(HttpStatusCode.Conflict);
      }
    }

    public HttpResponseMessage Put(TEntity entity)
    {
      try
      {
        if (!ModelState.IsValid)
          return Request.CreateResponse(HttpStatusCode.BadRequest);

        _db.Entry(entity).State = EntityState.Modified;

        _db.SaveChanges();

        return Request.CreateResponse(HttpStatusCode.OK);
      }
      catch (DbEntityValidationException)
      {
        return Request.CreateResponse(HttpStatusCode.BadRequest);
      }
      catch (DbUpdateConcurrencyException)
      {
        return Request.CreateResponse(HttpStatusCode.NotFound);
      }
      catch (DbUpdateException)
      {
        return Request.CreateResponse(HttpStatusCode.Conflict);
      }
    }

    public HttpResponseMessage Delete(TEntity entity)
    {
      try
      {
        _db.Entry(entity).State = EntityState.Deleted;

        _db.SaveChanges();

        return Request.CreateResponse(HttpStatusCode.OK);
      }
      catch (DbUpdateConcurrencyException)
      {
        return Request.CreateResponse(HttpStatusCode.NotFound);
      }
    }

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

DbSet :

public class CustomersController 
  : CrudController<Customer>
{
  public CustomersController()
    : base(db => db.Customers)
  {}
}

public class ProductsController 
  : CrudController<Product>
{
  public ProductsController()
    : base(db => db.Products)
  {}
}

:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}",
 );
+2

, . OData. RESTful , CRUD, . , OData -API, , , .

Entity.

+1

All Articles