4

I have problem with create ulr routing for asp.net mvc3 application.

My project has this structure :

  • Areas
    • EmployeeReport
      • Controllers
        • Report
      • Views
        • Report
          • List
          • ....
  • Controllers
    • Login
  • Viwes
    • Login
      • ...

EmployeeReportAreaRegistration.cs :

public class EmployeeReportAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "EmployeeReport";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        var routes = context.Routes;

        routes.MapRoute(null, "vykazy/vykazy-zamestnance", new { Area = "EmployeeReport", controller = "Report", action = "List" });

    }
}

Global.asax :

        routes.MapRoute(null, "prihlasit", new { controller = "Login", action = "Login" });

        routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Default", action = "Welcome", id = UrlParameter.Optional });

When i try load "http://localhost/app_name/vykazy/vykazy-zamestnance
i get this exception :

The view 'List' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Report/List.aspx
~/Views/Report/List.ascx
~/Views/Shared/List.aspx
~/Views/Shared/List.ascx
~/Views/Report/List.cshtml
~/Views/Report/List.vbhtml
~/Views/Shared/List.cshtml
~/Views/Shared/List.vbhtml

Well, where I do mistake ?

Thanks

Mennion
  • 2,873
  • 3
  • 20
  • 34

2 Answers2

1

revised answer:

Adding to Context.Routes directly means it loses any information about Areas.

Either use AreaRegistration.MapRoute (which is overriden to put in the Area information).

context.MapRoute(...);

Or put the area in the DataTokens parameter (not the defaults parameter as you have done here)

context.Routes.MapRoute("", "url", new {...}, null, new {area = this.AreaName});
Daryl Teo
  • 5,394
  • 1
  • 31
  • 37
  • Hmm, i have AreaRegistration.RegisterAllAreas() on top of Application_Start() function in global.asax – Mennion Sep 19 '11 at 13:42
  • Yes, i have folder EmployeeReportArea under Areas folder. Fixed in first comment. – Mennion Sep 19 '11 at 13:49
  • My mistake, the fact that it was searching for the view meant that it found the route. Try using http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx and see if this helps. – Daryl Teo Sep 19 '11 at 13:53
  • I have looked at my own code, and I do not specify "area" in my route registration... try removing that property. – Daryl Teo Sep 19 '11 at 13:58
  • Yes, Routing debbuger have installed and he says all is ok : True vykazy/vykazy-zamestnance Area = EmployeeReport, controller = Report, action = Lis (null) (null) – Mennion Sep 19 '11 at 14:02
  • And when I try debbug application, controller catch request correctly – Mennion Sep 19 '11 at 14:03
  • 1
    Do not add directly to Context.Routes. Use overriden MapRoute function, that handles Area for you. – Daryl Teo Sep 19 '11 at 14:07
  • I have revised my answer to match :) – Daryl Teo Sep 19 '11 at 14:15
0

Your folder structure for your area should look like so:

  • Areas
    • EmployeeReport
      • Controllers
      • Views
counsellorben
  • 10,924
  • 3
  • 40
  • 38