MVC routes and attribute routing

287 views Asked by At

I am trying to get some routing to work but I seem to have made a mistake somewhere and can't see it. A second set of eyes or explanation on what I did wrong would be appreciated.

Physical path:

/Views/Reports/Awards/Index.cshtml

Desired route: /Reports/Awards

My first attempt was to do a classic route map (see commented code) and also note that I did turn on routes.MapMvcAttributeRoutes():

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("wcf/{resource}.svc/{*pathInfo}");
        routes.IgnoreRoute("wcf/{resource}.svc");
        routes.MapMvcAttributeRoutes();
        // routes.MapRoute(
        //    name: "Reports",
        //    url: "Reports/{controller}/{action}/{id}",
        //    defaults: new { controller = "Awards", action = "Index", id = UrlParameter.Optional }
        //);
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

    }

The "traditional" route map did not work and that is when I stumbled across attribute routing and decided to give that a shot. Here is what the controller looks like:

[RoutePrefix("Reports/Awards")]
public class AwardsController : Controller 
{

    [Route]
    public ActionResult Index()
    {
}

If I move the physical location of the Awards folder everything seems to work. Views\Awards\Index.cshtml

So is the disconnect in the physical path? should I actually be implementing areas? (I would really prefer not to do that)

Thanks, Chris

0

There are 0 answers