我有以下控制器。两个控制器的所有方法都是相同的。我想制作一个通用的基本控制器,并从这个通用的基本控制器继承这两个控制器。任何人都可以帮助我。
课程控制器
public class CoursesController : ApiController
{
CourseRepository repository = new CourseRepository();
public List<Courseinfo> GetAllCourses()
{
var courses = repository.SelectAll();
var c = AutoMapper.Mapper.Map<List<Courseinfo>>(courses);
return c;
}
[ResponseType(typeof(Course))]
public IHttpActionResult GetCourseById(int id)
{
var courses = repository.SelectByID(id);
if (courses == null)
{
return NotFound();
}
var Courseifno = AutoMapper.Mapper.Map<Courseinfo>(courses);
return Ok(Courseifno);
}
// PUT: api/Courses/5
[ResponseType(typeof(void))]
public IHttpActionResult PutCourse(int id, Courseinfo courses)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != courses.CourseId)
{
return BadRequest();
}
//Student s = new Student();
var s = AutoMapper.Mapper.Map<Course>(courses);
repository.Update(s);
repository.Save();
return StatusCode(HttpStatusCode.NoContent);
}
//POST: api/Courses
[ResponseType(typeof(Course))]
public IHttpActionResult PostCourse(Courseinfo course)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var Courses = AutoMapper.Mapper.Map<Course>(course);
repository.Insert(Courses);
repository.Save();
return CreatedAtRoute("DefaultApi", new { id = course.CourseId }, course);
}
// DELETE: api/Courses/5
[ResponseType(typeof(Course))]
public IHttpActionResult DeleteCourse(int id)
{
Course Courses = new Course();
var cour = repository.SelectByID(id);
if (cour == null)
{
return NotFound();
}
repository.Delete(id);
repository.Save();
return Ok(Courses);
}
学生管理员
public class StudentsController : ApiController
{
MystudentRepository repository = new MystudentRepository();
public IEnumerable<Studentinfo> GetStudents()
{
var student = repository.SelectAll();
var studentinfo = AutoMapper.Mapper.Map<List<Studentinfo>>(student);
return studentinfo;
}
[ResponseType(typeof(Student))]
public IHttpActionResult GetStudents(int id)
{
var student = repository.SelectByID(id);
if (student == null)
{
// return NotFound();
string message = "No student exist";
throw new HttpResponseException(
Request.CreateErrorResponse(HttpStatusCode.NotFound, message));
}
var studentifno = AutoMapper.Mapper.Map<Studentinfo>(student);
return Ok(studentifno);
}
[ResponseType(typeof(Student))]
public IHttpActionResult PutStudent(int id, Studentinfo students)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != students.StudentId)
{
return BadRequest();
}
//Student s = new Student();
var s = AutoMapper.Mapper.Map<Student>(students);
repository.Update(s);
repository.Save();
return StatusCode(HttpStatusCode.NoContent);
}
[ResponseType(typeof(Student))]
public IHttpActionResult PostStudent(Studentinfo student)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var students = AutoMapper.Mapper.Map<Student>(student);
repository.Insert(students);
repository.Save();
return CreatedAtRoute("DefaultApi", new { id = student.StudentId }, student);
}
[ResponseType(typeof(Student))]
public IHttpActionResult DeleteStudent(int id)
{
Student student = new Student();
var Std = repository.SelectByID(id);
if (Std == null)
{
return NotFound();
}
repository.Delete(id);
repository.Save();
return Ok(student);
}
答案 0 :(得分:0)
您无法创建通用基本控制器,因为Web API中的属性路由不支持Route属性的继承。
作为一种解决方法,您可以将控制器逻辑移动到通用类,将实例添加到控制器,然后在控制器方法中调用泛型类方法。
例如:
public class RepositoryHandler<T> // generic base class that will incapsulate all "controllers generic logic"
{
private IRepository _repository;
public RepositoryHandler (IRepository repository)
{
_repository = repository;
}
public IEnumerable<T> GetAll()
{
var data = repository.SelectAll();
return AutoMapper.Mapper.Map<List<T>>(data);
}
}
public class CoursesController : ApiController
{
private RepositoryHandler<Courseinfo> _courseHandler;
public CoursesController()
{
IRepository repository = new CourseRepository();
_courseHandler = new RepositoryHandler<Courseinfo>(repository);
}
public List<Courseinfo> GetAllCourses()
{
var courseInfo = _courseHandler.GetAll();
return courseInfo.ToList();
}
}
public class StudentsController : ApiController
{
private RepositoryHandler<Studentinfo> _studentHandler;
public StudentsController()
{
IRepository repository = new MyStudentRepository();
_studentHandler = new RepositoryHandler<Studentinfo>(repository);
}
public List<Studentinfo> GetAllStudents()
{
var studentinfo = _studentHandler.GetAll();
return studentinfo.ToList();
}
}