Ich kann nicht auf meine ApiController

Ich versuche zum erstellen einer Api-controller verwendet für die Anmeldung, die verwendet werden sollten, vor mit meinem CustomerController (Api), um auf Daten zuzugreifen.

Das problem ist, dass ich immer einen 404 error, wenn ich versuche, auf meine Login-Methode auf AccountController. Ich bin versucht zu POSTEN, um die AccountController wie gezeigt in meinem screenshot unten.

Komische ist, dass ich in der Lage bin, um auf meinen CustomerController (Api) ohne jedes problem mit dem Hinweis, mein browser zu http://localhost:62655/api/customer/cvr/88888888. Vermisse ich eine convention oder etwas für die POST-Anforderung?

Meinen WebApi route config wird:

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 }
        );
    }
}

Hinzugefügt und in mein Global.asax:

WebApiConfig.Register(GlobalConfiguration.Configuration);

Meine AccountController und CustomerController sucht, wie so (Dateien zusammengeführt, der Kürze halber):

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;
    }
}

Ich kann nicht auf meine ApiController

Bild oben gibt einen 404-Fehler. Das Programm ist Fiddler2.

Ausnahme:

[HttpException]: Die Steuerung für Pfad '/api/account/login"
wurde nicht gefunden oder nicht implementiert IController.

Basierend auf Kommentare, update - (komplette Global.asax und 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 }
    );
}
  • webapi nicht wissen, dass Ihr Login () - Methode ist die Unterstützung für support POST
  • Von den blicken von Ihrem Konto controller, route und Anfrage..Sie scheinen gut, und Sie sollten bekommen haben eine Antwort...was für details kann man die 404-Antwort der Körper?...btw, ändern WebApiConfig.Register(GlobalConfiguration.Configuration); zu GlobalConfiguration.Configure(WebApiConfig.Register);
  • Auch wenn ich benennen Sie die Aktion um: Login([FromBody]LoginApiViewModel Modell) die ich noch erhalten, die eine 404. Aktualisiert die post mit Antwort-stack-strace.
  • Verändert auch die Globalen.asax ohne Glück
  • Umbenannt habe ich es zu PostLogin-ohne Glück.
  • Basierend auf Ihrer Ausnahme, scheint es, Ihre Anfrage wird ergänzt durch ein MVC-route und daher Ihre Suche für eine Implementierung von IController (MVC) eher als eine Implementierung von IHttpController (Web-API)...kannst du erzählen, wie deine Global.asax ist?
  • Ja, das ist sicher. Einen moment.
  • Ich habe eine MVC-controller namens AccountController, aber es erbt von Controller.
  • danke..ich gepostet eine Antwort..das problem ist mit der route config Bestellung..

InformationsquelleAutor janhartmann | 2014-01-06
Schreibe einen Kommentar