以下是从我的结尾定义的控制器:
namespace Demo.Web.Controllers.Api.Admin
{
public class UserAuthenticationController : ApiController
{
[HttpPost, ActionName(ActionNameConstants.PostAdminLogin)]
public IHttpActionResult PostAdminLogin(AdminLoginModel adminLoginModel)
{
return Ok(true);
}
}
}
和
namespace Demo.Web.Controllers.Api.Front
{
public class UserAuthenticationController : ApiController
{
[HttpPost]
[ActionName(ActionNameConstants.PostLogin)]
public void Post(FrontLoginModel frontLoginModel)
{
}
}
}
现在对于上述控制器,我们管理如下路由:
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "FrontApi",
routeTemplate: "api/front/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "AdminApi",
routeTemplate: "api/admin/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
对于上述控制器和路由,我面临以下错误:
Multiple types were found that match the controller named 'userAuthentication'.
This can happen if the route that services this request
('api/admin/{controller}/{action}/{id}') found multiple controllers
defined with the same name but differing namespaces, which is not supported.
请让我知道你的意见。
答案 0 :(得分:0)
已更新
将[Route("api/admin")]
添加到名称空间Demo.Web.Controllers.Api.Admin
中的控制器,将[Route("api/front")]
添加到名称空间Demo.Web.Controllers.Api.Front
中的控制器,并更改您的Register方法,如下所示:
namespace Demo.Web.Controllers.Api.Admin
{
[Route("api/admin")]
public class UserAuthenticationController : ApiController
{
[HttpPost,Route("login")]
public IHttpActionResult PostAdminLogin([FromBody]AdminLoginModel adminLoginModel)
{
return Ok(true);
}
}
}
和
namespace Demo.Web.Controllers.Api.Front
{
[Route("api/front")]
public class UserAuthenticationController : ApiController
{
[HttpPost]
[Route("login")]
public void Post([FromBody]FrontLoginModel frontLoginModel)
{
}
}
}
将您的void Register(HttpConfiguration配置)更改为以下内容:
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
var corsAttr = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(corsAttr);
}