Page not found Error 404, [Route("Categories/list/{page=1}")] in Asp.Net MVC

Asked

Viewed 185 times

1

Hello, I have a problem regarding the configuration of Route in Asp.Net MVC. The problem is that I created a test page where on this page I am trying to configure the route resources. I set up a search page with a method to create a url with the query parameter.

Ex:

http://localhost:59180/catetorias/list/1

The number "1" is the parameter being passed, and Padedlist who creates. And search works perfectly and it can change the url in Chrome, plus when I copy the url and paste in Microsoft Edge appears a 404 error page, page not found.

Controller: Categoriascontroller.Cs

using PartionView.DAO;
using PartionView.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;

namespace PartionView.Controllers
{

    public class CategoriasController : Controller
    {
        #region Carregamentos de páginas


        public ActionResult Index()
        {
            listCat();
            listUsers();
            return View();
        }

        /*
            O Endereço ficará:
            Get - categorias/list/{page}
        */
        [Route("Catetorias/list/{page=1}")]//Para acessar a função
        public ActionResult pagteste(int page = 1, int pageSize = 4)
        {
            listCatPg("", page, pageSize);
            return View();
        }

        [Route("Categorias/list/{pesquisa=}/{page=1}/{pageSize=4}")]
        public ActionResult pagteste(string pesquisa = "", int page = 1, int pageSize = 4)
        {
            listCatPg(pesquisa, page, pageSize);
            return View();
        }
        #endregion

        //===================================================================================

        #region Metodos Usados
        /// <summary>
        /// Método usado para lista todas as categórias.
        /// </summary>
        /// <param name="Pesquisa">Recebe a categoria para pesquisa.</param>
        /// <returns>Retorna lista na ViewBag de categorias.</returns>
        public ActionResult listCat(string Pesquisa = "")
        {
            var dao = new CategoriaDao();

            var list = dao.listaCategoria(Pesquisa);

            if (Request.IsAjaxRequest())
            {
                return PartialView("_PvCategorias", list);

            }

            ViewBag.cat = list;

            return View();
        }

        /// <summary>
        /// Método usado para lista todas as categórias.
        /// </summary>
        /// <param name="Pesquisa">Recebe a categoria para pesquisa.</param>
        /// <returns>Retorna lista na ViewBag de categorias.</returns>
        [HttpGet]
        public ActionResult listCatPg(string pesquisa = "", int page = 1, int pageSize = 4)
        {
            var dao = new CategoriaDao();

            var list = dao.listaCategoria(pesquisa);


            PagedList<Categoria> pl = new PagedList<Categoria>(list, page, pageSize);

            PesquisaFiltro pf = new PesquisaFiltro
            {
                categorias = pl,
                pesquisa = pesquisa,
                page = page,
                pageSize = pageSize
            };

            ViewBag.pf = pf;

            if (Request.IsAjaxRequest())
            {                
                return PartialView("_PvPagteste", pf);
            }

            ViewBag.cat = pf;

            return View(pf);
        }

        /// <summary>
        /// Método usado para lista todas as usuários.
        /// </summary>
        /// <param name="Pesquisa">Recebe a usuário para pesquisa.</param>
        /// <returns>Retorna lista na ViewBag de usuários.</returns>
        public ActionResult listUsers(string PesquisaUsers = "")
        {
            var dao = new CategoriaDao();

            var list = dao.listaUsuarios(PesquisaUsers);            

            if (Request.IsAjaxRequest())
            {
                return PartialView("_PvUsuarios", list);
            }

            ViewBag.us = list;

            return View();
        }
        #endregion

    }
}

In Routconfig.Cs and the pattern, I just added Mapmvcattributeroutes().

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        //Para mapear os atributos das rotas direto no controller.
        routes.MapMvcAttributeRoutes();

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

Things I’ve noticed doing tests:

  1. If you run the index page, and paste the url "http://localhost:59180/catetorias/list/1" already passing parameter 1, 2 or 3 it does not work.
  2. It doesn’t work either if you copy the above url and paste it into another browser.

If anyone knows it would be a good help.

1 answer

-1

I found out, it was just a typo:

[Route("Catetorias/list/{page=1}")]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.