Error with multiples MapRoutes

May 5, 2011 at 1:32 PM
Edited May 5, 2011 at 2:01 PM
Mvc.sitemap
<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-3.0" enableLocalization="false">
    <mvcSiteMapNode controller="Empreendimento" action="Lista" title="Empreendimentos" key="Lista" changeFrequency="Always" updatePriority="Normal">
      <mvcSiteMapNode action="Buscar" title="Busca" key="Buscar" />
      <mvcSiteMapNode action="Detalhes" title="Detalhes do empreendimento" >
        <mvcSiteMapNode action="Editar" title="Edição do empreendimento" />
      </mvcSiteMapNode>
      <mvcSiteMapNode action="Novo" title="Novo empreendimento" />
    </mvcSiteMapNode>
</mvcSiteMap>
Global.asax
public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 
    routes.MapRoute(
        "Lista",
        "{controller}/Lista/{pagina}/{coluna}/{ordem}",
        new { controller = "Empreendimento", action = "Lista", pagina = UrlParameter.Optional, coluna = UrlParameter.Optional, ordem = UrlParameter.Optional } // Parameter defaults
    );
 
    routes.MapRoute(
        "OrdenarPor",
        "{controller}/OrdenarPor/{pagina}/{coluna}/{ordem}",
        new { controller = "Empreendimento", action = "OrdenarPor", pagina = UrlParameter.Optional, coluna = UrlParameter.Optional, ordem = UrlParameter.Optional } // Parameter defaults
    );
 
    routes.MapRoute(
        "Buscar",
        "{controller}/Buscar/{pagina}/{coluna}/{ordem}",
        new { controller = "Empreendimento", action = "Buscar", pagina = UrlParameter.Optional, coluna = UrlParameter.Optional, ordem = UrlParameter.Optional } // Parameter defaults
    );
 
    routes.MapRoute(
        "Default"// Route name
        "{controller}/{action}/{id}"// URL with parameters
        new { controller = "Empreendimento", action = "Lista", id = UrlParameter.Optional } // Parameter defaults
    );
 
}
EmpreendimentoController
[HttpGet]
[SiteMapPreserveRouteData]
[MvcSiteMapNodeAttribute(Key="Lista")]
public override ActionResult Lista(int? pagina, string coluna, string ordem)
{
//My code
}
        
[HttpGet]
[MvcSiteMapNodeAttribute]
public override ActionResult OrdenarPor(int? pagina, string q, string coluna, string ordem)
{
//My code
}
        
[HttpGet]
[MvcSiteMapNodeAttribute]
public ActionResult Novo()
{
//My code
}
 
[HttpPost]
public ActionResult Novo(Empreendimento model)
{
//My code
}
 
[HttpGet]
[SiteMapPreserveRouteData]
[MvcSiteMapNodeAttribute(Key="Editar")]
public ActionResult Editar(int? id)
{
//My code
}
 
[HttpPost]
public ActionResult Editar(Empreendimento model)
{
//My code
}
 
[HttpGet]
[SiteMapPreserveRouteData]
[MvcSiteMapNodeAttribute(Key="Buscar")]
public ActionResult Buscar(int? pagina, string coluna, string ordem, string q)
{
//My code
}
 
[HttpGet]
[SiteMapPreserveRouteData]
[MvcSiteMapNodeAttribute(Key="Detalhes")]
public ActionResult Detalhes(int? id)
{
//My code
}
_Layout.cshtml
<div>
    @Html.MvcSiteMap().SiteMapPath()
</div>

When I navigating through the "Detalhes" and "Editar", it works fine, I get a render like this: "Empreendimentos > Detalhes do empreendimento > Editação do empreendimento" (As I want)

But when I use the "Buscar" or "Lista" it doesn't work if I don't use the full rote map.

 

Works: http://localhost/Application/Empreendimento/Lista/2/Nome/DESC

Doesn't work: http://localhost/Application/Empreendimento/Lista/2/

 

I'm paging this list, so in certain moment I'll use the url without the two last route values, but If I don't I get the error:

 

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   MvcSiteMapProvider.DefaultSiteMapProvider.FindSiteMapNode(HttpContext context, RouteData routeData) +285
   System.Web.SiteMapProvider.get_CurrentNode() +68
   MvcSiteMapProvider.Filters.SiteMapPreserveRouteDataAttribute.OnActionExecuted(ActionExecutedContext filterContext) +107
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +262
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +784922
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +314
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +784976
   System.Web.Mvc.Controller.ExecuteCore() +159
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +335
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +62
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +54
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +371

 

I hope you understand my problem. Thanks.