jan*_*ann 9 c# asp.net-mvc asp.net-web-api
我正在尝试创建一个用于登录的Api控制器,应该在使用my CustomerController(Api)访问数据之前使用.
问题是当我尝试访问我的Login方法时遇到404错误AccountController.我正在尝试POST,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 }
);
}
}
Run Code Online (Sandbox Code Playgroud)
并添加到我的Global.asax中:
WebApiConfig.Register(GlobalConfiguration.Configuration);
Run Code Online (Sandbox Code Playgroud)
我的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;
}
}
Run Code Online (Sandbox Code Playgroud)

上面的图像返回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 }
);
}
Run Code Online (Sandbox Code Playgroud)
Kir*_*lla 25
将您的配置更改为如下所示...这里我在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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14088 次 |
| 最近记录: |