I am create a authentication WebAPI.
in APP_Start Folder:
WebApiConfig file:
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 file:
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); }
I have the AuthenticationController in my controllers folder. I am not sure what am I doing wrong. I have a few other APIs that work fine.
In the url I put the url I am entering is localhost:4453/api/authentication/user/pass. if I only enter user I get.
{"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; }
Global.aspx必须有这条线
WebApiConfig.Login(GlobalConfiguration.Configuration);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.