我正在尝试创建一个用于登录的Api控制器,在使用我的CustomerController
(Api)访问数据之前应使用该控制器。
问题是,当我尝试访问上的登录方法时,出现404错误AccountController
。我正在尝试发布到,AccountController
如下面的屏幕快照所示。
有趣的是,CustomerController
通过将浏览器指向,我可以毫无问题地访问(Api)http://localhost:62655/api/customer/cvr/88888888
。我是否缺少POST请求的约定或内容?
我的WebApi路由配置为:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
并添加到我的Global.asax中:
WebApiConfig.Register(GlobalConfiguration.Configuration);
我的AccountController和CustomerController看起来像这样(为简洁起见,合并了文件):
public class AccountController : ApiController
{
public UserManager<ApplicationUser> UserManager { get; private set; }
private IAuthenticationManager AuthenticationManager
{
get
{
return HttpContext.Current.GetOwinContext().Authentication;
}
}
public AccountController(UserManager<ApplicationUser> userManager)
{
UserManager = userManager;
}
public async Task<HttpResponseMessage> Login([FromBody]LoginApiViewModel model)
{
if (!ModelState.IsValid) return Request.CreateResponse(HttpStatusCode.BadRequest, "Username or password is not supplied");
var user = UserManager.Find(model.Username, model.Password);
if (user != null && UserManager.IsInRole(user.Id, "Administrator"))
{
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true }, identity);
var response = Request.CreateResponse(HttpStatusCode.OK, "Success");
return response;
}
return Request.CreateResponse(HttpStatusCode.Unauthorized, "Wrong login");
}
}
[Authorize(Roles = "Administrator")]
public class CustomerController : ApiController
{
private readonly ICustomerService _customerService;
public CustomerController(ICustomerService customerService)
{
_customerService = customerService;
}
[ActionName("cvr")]
public CustomerApiViewModel GetCustomerById(string id)
{
var customer = _customerService.GetByCVR(id);
if (customer == null) throw new HttpResponseException(HttpStatusCode.NotFound);
var customerViewModel = Mapper.Map<CustomerApiViewModel>(customer);
return customerViewModel;
}
}
上图返回404错误。该程序是Fiddler2。
例外:
[HttpException]:找不到路径'/ api / account / login'的控制器或未实现IController。
基于注释,更新-(完整的Global.asax和RouteConfig(MVC)
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
GlobalConfiguration.Configure(WebApiConfig.Register);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AutoMapperWebConfiguration.Configure();
GlobalConfiguration.Configuration.EnsureInitialized();
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.LowercaseUrls = true;
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"DefaultOnlyAction",
"{action}",
new { controller = "Home", action = "Index" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
更改您的配置,如下所示...这里我在MVC路由之前移动了Web api路由...这是因为Web API路由比通用MVC路由更具体。
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AutoMapperWebConfiguration.Configure();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句