我正在创建一个身份验证WebAPI。
APP_Start文件夹中的:
WebApiConfig文件:
public static string UrlPrefix
{
get
{
return "api";
}
}
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute("DefaultApi", string.Concat(WebApiConfig.UrlPrefix, "/{controller}/{id}"), new { id = RouteParameter.Optional });
MediaTypeHeaderValue mediaTypeHeaderValue = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault<MediaTypeHeaderValue>((MediaTypeHeaderValue t) => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(mediaTypeHeaderValue);
}
public static void Login(HttpConfiguration config)
{
config.Routes.MapHttpRoute("Login", string.Concat(WebApiConfig.UrlPrefix, "/{controller}/{username}/{password}"), new { username = RouteParameter.Optional, password = RouteParameter.Optional });
MediaTypeHeaderValue mediaTypeHeaderValue = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault<MediaTypeHeaderValue>((MediaTypeHeaderValue t) => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(mediaTypeHeaderValue);
}
WebApiApplication文件:
protected void Application_PostAuthorizeRequest()
{
HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
WebApiConfig.Login(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
我在controllers文件夹中有AuthenticationController。我不确定我做错了什么。我有一些其他API工作正常。
在网址中,我输入的网址是localhost:4453 / api / authentication / user / pass。如果我只输入用户我得到。
{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:4453/api/Authentication/asas/'.","MessageDetail":"No action was found on the controller 'Authentication' that matches the request."}
AuthenticationController:
public bool Get(string username, string password)
{
return true;
}
答案 0 :(得分:0)
Global.aspx必须有这一行
WebApiConfig.Login(GlobalConfiguration.Configuration);