Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
ASP .NET Core MVC JQuery AJAX: problema al obtener datos para ver a través del controlador api usando ajax

Cuando se ejecuta la línea getJSON y hay enrutamiento, por ejemplo: https://localhost:44338/ArticleApi/GetNextArticles?id=12&quantity=2&filterName=Odziez

me sale error:

Error al cargar el recurso: el servidor respondió con un estado de 404 ()

No se puede encontrar esta página en localhost

Método GetNextArticles en mi ArticleApiController:

 [HttpGet("{id}/{count}/{filterName}")] public IEnumerable<Article> GetNextArticles(int id, int count, string filterName) { return _articleRepository.GetNextArticles(id, count, filterName); }

en mi opinión (archivo .cshtml):

 $.getJSON('@Url.Action("GetNextArticles", "ArticleApi")' + "?id=" + last + "&quantity=" + quantity + "&filterName=" + filterCategory, function(data, status) { for(var index in data) {etc...}}

Método GetNextArtículos:

 public IEnumerable<Article> GetNextArticles(int id, int count, string filterName) { List<Article> articles = new List<Article>(); var categoryId = _context.Categories.Where(c => c.CategoryName == filterName).Select(c => c.CategoryId).FirstOrDefault(); for (int i = 0; i < count; i++) { var foundArticles = _context.Articles.Where(a => a.CategoryId == categoryId && a.ArticleId == id + i + 1).ToList(); if (foundArticles.Count() == 0) { return articles; } var article = foundArticles[0]; if (article != null) { article.Category = _context.Categories.Find(article.CategoryId); articles.Add(article); } else { var nextArticles = _context.Articles.Where(a => a.CategoryId == categoryId && article.ArticleId <= id + i + 100).ToList(); if (nextArticles.Count != 0) { id = nextArticles.Min(a => a.ArticleId); articles.Add(nextArticles.Where(a => a.ArticleId == id).First()); } else { break; } } } return articles; }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Según el error, podría validar los nombres de los parámetros que está enviando. Revisando el código me doy cuenta que uno de ellos se envía con el nombre "cantidad" y en el controlador lo llamas "contar"

about 4 years ago · Juan Pablo Isaza Report

0

Si usa [HttpGet("{id}/{count}/{filterName}")] en su API, la URL que se pasa a esta API debe ser https://localhost:44338/ArticleApi/GetNextArticles/Id/count/filterName .

Entonces, si no desea cambiar $.getJSON(..){...} en la Vista, puede cambiar la API como:

 [HttpGet] public IEnumerable<Article> GetNextArticles([FromQuery]int id, int count, string filterName) { return _articleRepository.GetNextArticles(id, count, filterName); }

Puede ver que la Api funciona bien y también se puede cargar en Swagger

ingrese la descripción de la imagen aquí ingrese la descripción de la imagen aquí

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!