为了适应控制器中的多个get方法,我需要在Web API配置文件中修改什么
本文关键字:Web 修改 配置文件 API 什么 方法 控制器 get | 更新日期: 2023-09-27 18:29:48
我有一个Web API,其中我在控制器中有多个get方法
以下是我的代码:
我的存储库类:
public class myRepository
{
public myClasses.Type[] GetAllTypes()
{
return new myClasses.Type[]
{
new myClasses.Type
{
typeId="1",
typeVal = "New"
},
new myClasses.Type
{
typeId="2",
typeVal = "Old"
}
};
}
public myClasses.Employee[] GetAllEmployees()
{
return new myClasses.Employee[]
{
new myClasses.Employee
{
empId="111111",
empFName = "Jane",
empLName="Doe"
},
new myClasses.Employee
{
empId="222222",
empFName = "John",
empLName="Doe"
}
};
}
public bool VerifyEmployeeId(string id)
{
myClasses.Employee[] emp = new myClasses.Employee[]
{
new myClasses.Employee
{
empId="111111",
empFName = "Jane",
empLName="Doe"
},
new myClasses.Employee
{
empId="222222",
empFName = "John",
empLName="Doe"
}
};
for (var i = 0; i <= emp.Length - 1; i++)
{
if (emp[i].empId == id)
return true;
}
return false;
}
}
和我的模型类:
public class myClasses
{
public class Employee
{
public string empId { get; set; }
public string empFName { get; set; }
public string empLName { get; set; }
}
public class Type
{
public string typeId { get; set; }
public string typeVal { get; set; }
}
}
这是我的控制器:
public class myClassesController : ApiController
{
private myRepository empRepository;
public myClassesController()
{
this.empRepository = new myRepository();
}
public myClasses.Type[] GetTypes()
{
return empRepository.GetAllTypes();
}
public myClasses.Employee[] GetEmployees()
{
return empRepository.GetAllEmployees();
}
public bool VerifyEmployee(string id)
{
return empRepository.VerifyEmployeeId(string id);
}
}
一切都编译得很好,但当我使用运行它时
http://localhost:49358/api/myClasses/GetTypes
我收到一个错误,说明
找到多个与请求匹配的操作:类型myProject.Controllers.myClasssController Employee[]GetEmployees()对类型myProject.Controllers.MyClasseController
有人能帮我更新配置文件吗?
我的WebAPIConfig:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
//config.EnableQuerySupport();
// To disable tracing in your application, please comment out or remove the following line of code
// For more information, refer to: http://www.asp.net/web-api
config.EnableSystemDiagnosticsTracing();
}
}
更新:找到了答案!正如我所怀疑的,修复程序在WebApiConfig中。我修改了默认路线,一切正常。
我可能会使用路由属性来实现这一点(当然,我总是使用路由属性,我喜欢显式)。
首先,在任何声明的路由之前将此行添加到WebApiConfig.cs中:
config.MapHttpAttributeRoutes();
然后,用路由属性和HttpVerbs:装饰控制器操作
[HttpGet]
[Route("api/myClasses/GetTypes")]
public myClasses.Type[] GetTypes()
{
return empRepository.GetAllTypes();
}
[HttpGet]
[Route("api/myClasses/GetEmployees")]
public myClasses.Employee[] GetEmployees()
{
return empRepository.GetAllEmployees();
}
您可以在此处阅读有关属性路由的更多信息:http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2
找到了答案!正如我所怀疑的,修复程序在WebApiConfig中。我修改了默认路线,一切正常。