I am using routes.MapMvcAttributeRoutes();
to decorate my Actions with Url that I want to appear in the browser, and it has worked very well, however, when using PagedList
I have a problem that I can not solve.
Below is the code for my controller:
[Route("sala-de-impresa/noticias/{page?}")]
public ActionResult Noticia(int? page)
{
var model = _ctx.Noticias.Include("Categoria").Where(n => n.Status && n.DataPublicacao <= DateTime.Now).OrderByDescending(n => n.NoticiaId);
const int pageSize = 20;
var pageNumber = (page ?? 1);
return View(model.ToPagedList(pageNumber, pageSize));
}
Here is the View code that pagination:
@Html.PagedListPager(Model, page => Url.Action("Noticia", new { page, currentFilter = ViewBag.CurrentFilter }), PagedListRenderOptions.OnlyShowFivePagesAtATime)
For the Home page the Url is accessed without problems,
Ex: localhost: xxxx / print-room / news
But when trying to advance pagination to page 2 for example:
localhost: xxxx / print-room / news / 2
I get the error:
Erro de Servidor no Aplicativo '/'.
Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL.
The request has found the following matching controller types:
WP_AMERON.Controllers.NoticiaController
WP_AMERON.Controllers.SalaImprensaController
Descrição: Ocorreu uma exceção sem tratamento durante a execução da atual solicitação da Web. Examine o rastreamento de pilha para obter mais informações sobre o erro e onde foi originado no código.
Detalhes da Exceção: System.InvalidOperationException: Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL.
The request has found the following matching controller types:
WP_AMERON.Controllers.NoticiaController
WP_AMERON.Controllers.SalaImprensaController
Erro de Origem:
Exceção sem tratamento foi gerada durante a execução da atual solicitação da Web. As informações relacionadas à origem e ao local da exceção podem ser identificadas usando-se o rastreamento de pilha de exceção abaixo.
Rastreamento de Pilha:
[InvalidOperationException: Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL.
The request has found the following matching controller types:
WP_AMERON.Controllers.NoticiaController
WP_AMERON.Controllers.SalaImprensaController]
System.Web.Mvc.DefaultControllerFactory.GetControllerTypeFromDirectRoute(RouteData routeData) +373
System.Web.Mvc.DefaultControllerFactory.GetControllerType(RequestContext requestContext, String controllerName) +141
System.Web.Mvc.DefaultControllerFactory.System.Web.Mvc.IControllerFactory.GetControllerSessionBehavior(RequestContext requestContext, String controllerName) +53
System.Web.Mvc.MvcRouteHandler.GetSessionStateBehavior(RequestContext requestContext) +131
System.Web.Mvc.MvcRouteHandler.GetHttpHandler(RequestContext requestContext) +33
System.Web.Mvc.MvcRouteHandler.System.Web.Routing.IRouteHandler.GetHttpHandler(RequestContext requestContext) +10
System.Web.Routing.UrlRoutingModule.PostResolveRequestCache(HttpContextBase context) +9767524
System.Web.Routing.UrlRoutingModule.OnApplicationPostResolveRequestCache(Object sender, EventArgs e) +82
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69
Informações sobre a Versão: Microsoft .NET Framework Versão:4.0.30319; Versão do ASP.NET:4.0.30319.33440
But if I edit Url and put it like this:
localhost: xxxx / print-room / news? page = 2
Paging works normally.
EDIT:
Controller Code News and Company Room
public class SalaImprensaController : Controller
{
readonly AmeronContext _ctx = new AmeronContext();
[Route("sala-de-impresa/noticias/{page?}")]
public ActionResult Noticia(int? page)
{
var model = _ctx.Noticias.Include("Categoria").Where(n => n.Status && n.DataPublicacao <= DateTime.Now).OrderByDescending(n => n.NoticiaId);
const int pageSize = 20;
var pageNumber = (page ?? 1);
return View(model.ToPagedList(pageNumber, pageSize));
}
[Route("sala-de-impresa/videos")]
public ActionResult Videos(int? page)
{
var model = _ctx.Videos.ToList().OrderByDescending(n => n.VideoId).ToList();
const int pageSize = 9;
var pageNumber = (page ?? 1);
return View(model.ToPagedList(pageNumber, pageSize));
}
[Route("sala-de-impresa/galerias/{page?}")]
public ActionResult Galerias(int? page)
{
var model = _ctx.Galerias.ToList().OrderByDescending(n => n.GaleriaId).ToList();
const int pageSize = 9;
var pageNumber = (page ?? 1);
return View(model.ToPagedList(pageNumber, pageSize));
}
}
public class NoticiaController : Controller
{
readonly AmeronContext _ctx = new AmeronContext();
public ActionResult Index()
{
return View();
}
public ActionResult CarouselNoticias()
{
var model = _ctx.Noticias.Include("Categoria").Where(n => n.Status && n.DataPublicacao <= DateTime.Now && n.Destaque).OrderByDescending(n => n.NoticiaId);
return PartialView("_Carroussel", model);
}
public ActionResult Noticias()
{
var model = _ctx.Noticias.Include("Categoria").Where(n => n.Status && n.DataPublicacao <= DateTime.Now && n.Destaque == false).OrderByDescending(n => n.NoticiaId);
return PartialView("_Noticias", model);
}
[Route("{categoria}/{id}/{titulo}")]
public ActionResult Details(int id)
{
var noticia = _ctx.Noticias.Include("Categoria").FirstOrDefault(n => n.NoticiaId == id);
if (noticia == null)
{
return RedirectToAction("NotFound");
}
if (noticia.ViewNumber == null)
{
noticia.ViewNumber = 1;
}
else
{
noticia.ViewNumber++;
}
_ctx.Entry(noticia).State = EntityState.Modified;
_ctx.SaveChanges();
var categoria = noticia.Categoria.Nome;
var widget = _ctx.Noticias.Include("Categoria").ToList().Where(n => n.Categoria.Nome == categoria && n.Status).OrderByDescending(n => n.NoticiaId).Take(15);
ViewBag.Noticia = noticia;
return View(widget);
}