Error "No type was found that matches the controller named" WebApi

0

Because the error "No type was found that matches the controller named", does not recognize my controller, My WebApiConfig.cs here is

  public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            // Configure Web API to use only bearer token authentication.
            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                    //routeTemplate: "api/{controller}/{id}",
                    routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

And my Controller

 [Authorize]
    [RoutePrefix("api/Account")]
[HttpGet]
        public static String GetMotivos()
        {
            XmlSerializer xml_serializer = new XmlSerializer(typeof(oLogin));
            StringReader string_reader = new StringReader(CookieUtils.LeeValorCookie(System.Web.Configuration.WebConfigurationManager.AppSettings["nombreCookie"]));
            oLogin myLogin = (oLogin)xml_serializer.Deserialize(string_reader);
            string_reader.Close();

            cTripleDES des = new cTripleDES();
            String regresa = String.Empty;
            String sQuery = String.Format("SELECT llave, Nombre FROM promMotivos WHERE Borrado = 0 AND tipoMotivo = 0 AND llaveEmp = '{0}' ORDER BY Nombre", myLogin.Empresa);
            DataSet ds = bdBase.bdDataset(des.Decrypt(myLogin.cadConexion), sQuery);
            //regresa = "<option value='00000000-0000-0000-0000-000000000000'></option> ";
            foreach (DataRow renglon in ds.Tables[0].Rows)
            {
                regresa += String.Format("<option value='{0}'>{1}</option> ", renglon["llave"].ToString().Trim(), renglon["Nombre"].ToString().Trim());
            }
            return regresa;
        }
        [HttpPost]
        public static String ReportaVale(String idVale, String idMotivo)
        {
            XmlSerializer xml_serializer = new XmlSerializer(typeof(oLogin));
            StringReader string_reader = new StringReader(CookieUtils.LeeValorCookie(System.Web.Configuration.WebConfigurationManager.AppSettings["nombreCookie"]));
            oLogin myLogin = (oLogin)xml_serializer.Deserialize(string_reader);
            string_reader.Close();

            cTripleDES des = new cTripleDES();
            String regresa = String.Empty;
            //String sQuery = String.Format("UPDATE VALES SET IdMotivoPerdido = {0}, idUsuarioCancela = {1}, FechaCancela = GetDate() WHERE id = {2}", idMotivo, myLogin.idPromotor, idVale);
            String sQuery = String.Format("UPDATE promVales SET [status] = 5, llaveMotivoPerdido = '{0}', llaveUsuarioCancela = '{1}', FechaCancela = GetDate() WHERE llave = '{2}'",
                                          idMotivo, myLogin.idPromotor, idVale);
            regresa = bdBase.bdExecute(des.Decrypt(myLogin.cadConexion), sQuery, false, false);
            if (String.IsNullOrEmpty(regresa)) regresa = "Ok"; else regresa = "Error: " + regresa;

            return regresa;
        }
        [HttpPost]
        public static string GetData(DateTime Del, DateTime Al)
        {
            XmlSerializer xml_serializer = new XmlSerializer(typeof(oLogin));
            StringReader string_reader = new StringReader(CookieUtils.LeeValorCookie(System.Web.Configuration.WebConfigurationManager.AppSettings["nombreCookie"]));
            oLogin myLogin = (oLogin)xml_serializer.Deserialize(string_reader);
            string_reader.Close();

            cTripleDES des = new cTripleDES();
            String sQuery = String.Format("SELECT promVales.llave, promVales.NumeroVale, promVales.llaveProm, LTRIM(RTRIM(LTRIM(RTRIM(ISNULL(crmCustomers.Numero, ''))) + ' ' + " +
                                          "LTRIM(RTRIM(ISNULL(crmCustomers.Nombre1, ''))) + ' ' + LTRIM(RTRIM(ISNULL(crmCustomers.Nombre2, ''))) + ' ' + " +
                                          "LTRIM(RTRIM(ISNULL(crmCustomers.Apellido1, ''))) + ' ' + LTRIM(RTRIM(ISNULL(crmCustomers.Apellido1, ''))))) AS Nombre, " +
                                          "crmCustomers.DireccionCompleta, CrmCustomers.Telefono, CASE WHEN promVales.Status = 1 THEN 'Asignado' WHEN promVales.Status = 2 THEN 'Extraviado' " +
                                          "WHEN promVales.Status = 3 THEN 'Utilizado' WHEN promVales.Status = 4 THEN 'Suspendido por Depto. de Crédito' WHEN promVales.Status = 5 THEN " +
                                          "'Cancelado' ELSE 'Este vale no puede ser utilizado' END AS Status, promVales.FechaAsigna, aztiendas.NOMBRE AS TiendaAsigna, promVales.FechaUso, " +
                                          "aztiendas_1.NOMBRE AS TiendaUso, Personas.Nombre AS NombreUsuarioUso, LTRIM(RTRIM(ISNULL(login.Nombres, '') + ' ' + " +
                                          "LTRIM(RTRIM(ISNULL(login.Apellidos, ''))))) AS AsignadoPor, LTRIM(RTRIM(ISNULL(login_1.Nombres, '') + ' ' + " +
                                          "LTRIM(RTRIM(ISNULL(login_1.Apellidos, ''))))) AS UsuarioVenta, promVales.Caja, promVales.Operacion, promMotivos.Nombre AS MotivoValeExtraviado, " +
                                          "promMotivos_1.Nombre AS MotivoValeCancelado, promVales.Monto, promVales.MontoAutoriza, promVales.Anticipo FROM promVales LEFT JOIN CrmCustomers ON " +
                                          "promVales.llaveProm = crmCustomers.id LEFT JOIN azTiendas ON promVales.llaveTiendaAsigna = azTiendas.llave LEFT JOIN azTiendas AS azTiendas_1 ON " +
                                          "promVales.llaveTiendaUso = azTiendas_1.llave LEFT JOIN Personas ON promVales.llaveClienteUso = Personas.llave LEFT JOIN login ON " +
                                          "promVales.llaveUsuarioAsigna = login.llave LEFT JOIN login AS login_1 ON promVales.llaveUsuarioVenta = login_1.llave LEFT JOIN promMotivos ON " +
                                          "promVales.llaveMotivoPerdido = promMotivos.llave LEFT JOIN promMotivos AS promMotivos_1 ON promVales.llaveMotivoCancela = promMotivos.llave WHERE " +
                                          "(DATEADD(DD, 0, DATEDIFF(DD, 0, promVales.FechaAsigna)) BETWEEN '{0}' AND '{1}') AND promVales.llaveProm = '{2}'",
                                          String.Format("{0:yyyyMMdd}", Del), String.Format("{0:yyyyMMdd}", Al), myLogin.idPromotor);
            DataSet ds = bdBase.bdDataset(des.Decrypt(myLogin.cadConexion), sQuery);
            StringBuilder sb = new StringBuilder();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                Boolean hasMoreRecords = false;
                sb.Append(@"{" + "\"sEcho\": 1,");
                sb.Append("\"iTotalRecords\": " + GetRowCount("Vales", "WHERE ((DATEADD(DD, 0, DATEDIFF(DD, 0, Vales.FechaAsigna)) BETWEEN '" + String.Format("{0:yyyyMMdd}", Del) +
                                                              "' AND '" + String.Format("{0:yyyyMMdd}", Al) + "')) AND (Vales.FechaUso IS NULL) AND (Vales.IdPromotor = " + myLogin.idPromotor + ")") + ",");  // ds.Tables[0].Rows.Count + ",");
                sb.Append("\"iTotalDisplayRecords\": " + ds.Tables[0].Rows.Count + ",");
                sb.Append("\"aaData\": [");
                foreach (DataRow renglon in ds.Tables[0].Rows)
                {
                    if (hasMoreRecords) sb.Append(",");

                    sb.Append("[");
                    //sb.Append("\"<a class='edit' href='javascript:;'>Editar </a>\",");
                    string tmpString = HttpUtility.JavaScriptStringEncode("\"" + renglon["llave"].ToString() + "\"");
                    string tmpString2 = HttpUtility.JavaScriptStringEncode("\"" + renglon["NumeroVale"].ToString() + "\"");
                    if (renglon["Status"].ToString() == "Asignado")
                        sb.Append("\"<a class='delete' href='javascript:Reportar(" + tmpString + ", " + tmpString2 + ");'>Cancelar </a>\",");
                    else sb.Append("\"\",");
                    //sb.Append("\"<input type='checkbox' class='checkboxes' value='" + renglon["Id"].ToString() + "'/>\",");
                    if (!Convert.IsDBNull(renglon["llave"])) sb.Append("\"" + renglon["llave"].ToString().Trim() + "\","); else sb.Append("\"\",");
                    if (!Convert.IsDBNull(renglon["NumeroVale"])) sb.Append("\"" + renglon["NumeroVale"].ToString().Trim() + "\","); else sb.Append("\"\",");
                    if (!Convert.IsDBNull(renglon["Status"])) sb.Append("\"" + renglon["Status"].ToString().Trim() + "\","); else sb.Append("\"\",");
                    if (!Convert.IsDBNull(renglon["MotivoValeExtraviado"])) sb.Append("\"" + renglon["MotivoValeExtraviado"].ToString().Trim() + "\","); else sb.Append("\"\",");
                    if (!Convert.IsDBNull(renglon["FechaAsigna"]))
                    {
                        DateTime tmpDate = DateTime.Parse(renglon["FechaAsigna"].ToString());
                        sb.Append("\"" + String.Format("{0:dd-MM-yyyy}", tmpDate) + "\",");
                    }
                    else sb.Append("\"\",");
                    if (!Convert.IsDBNull(renglon["TiendaAsigna"])) sb.Append("\"" + renglon["TiendaAsigna"].ToString().Trim() + "\","); else sb.Append("\"\",");
                    if (!Convert.IsDBNull(renglon["AsignadoPor"])) sb.Append("\"" + renglon["AsignadoPor"].ToString().Trim() + "\""); else sb.Append("\"\"");
                    sb.Append("]");
                    hasMoreRecords = true;
                }
                sb.Append("]}");
            }
            else
            {
                sb.Append(@"{" + "\"sEcho\": 1,");
                sb.Append("\"iTotalRecords\": " + ds.Tables[0].Rows.Count + ",");
                sb.Append("\"iTotalDisplayRecords\": " + ds.Tables[0].Rows.Count + ",");
                sb.Append("\"aaData\": [");
                sb.Append("]}");
            }
            String temp = sb.ToString();
            return sb.ToString();
        }
        [HttpPost]
        private static int GetRowCount(String tabla, String condicion)
        {
            XmlSerializer xml_serializer = new XmlSerializer(typeof(oLogin));
            StringReader string_reader = new StringReader(CookieUtils.LeeValorCookie(System.Web.Configuration.WebConfigurationManager.AppSettings["nombreCookie"]));
            oLogin myLogin = (oLogin)xml_serializer.Deserialize(string_reader);
            string_reader.Close();

            int regresa = -1;
            cTripleDES des = new cTripleDES();
            String sQuery = String.Format("SELECT COUNT(*) AS Cuantos FROM {0} {1}", tabla, condicion);
            String regresaX = bdBase.bdExecute(des.Decrypt(myLogin.cadConexion), sQuery);
            if (utilerias.IsNumeric(regresaX)) regresa = int.Parse(regresaX);
            return regresa;
        }

    
asked by Oscar Navarro 25.09.2017 в 22:14
source

1 answer

1

I found my error in the WebApiConfig.cs

 public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            // Configure Web API to use only bearer token authentication.
            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

            // Web API routes
            config.MapHttpAttributeRoutes();
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                    //routeTemplate: "api/{controller}/{action}/{id}",
                    routeTemplate: "api/{controller}/{action}", Cambié a ésta línea , quitando el parámetro {id}
                defaults: new { id = RouteParameter.Optional }
            );
        }

and at the time of calling the method in the URL I made the error that I wrote:

link

instead of:

link

I did not know that the reserved word "controller" had to be removed from the url.

    
answered by 25.09.2017 / 23:21
source