Scott 混合理论
Scott 混合理论

Reputation: 2332

asp.net-mvc's ROUTE: The resource cannot be found

I'm using asp.net mvc, but can't access my page, got 404 error.

page's url :

localhost:2334/RawData/EiphoneNews

view file's location:

webroot/View/RawData/TNews/Index.cshtml

my route:

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

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

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

        routes.MapRoute(
           "RawData", // Route name
           "RawData/{controller}/{action}/{id}", // URL with parameters
           new { controller = "EiphoneNews", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
           new string[] { "News.Controllers.RawData" }
       );
    }




    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

my controller:

namespace News.Controllers.RawData
{
    public class EiphoneNewsController : AuthorizedController
    {
        //
        // GET: /EiphoneNews/
        public ActionResult Index(int pagenum = 0, int pagesize = 20, string queryString = null)
        {...}
    }
}

Upvotes: 1

Views: 7634

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038800

Swap the 2 route definitions:

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

    routes.MapRoute(
       "RawData", // Route name
       "RawData/{controller}/{action}/{id}", // URL with parameters
       new { controller = "EiphoneNews", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
       new string[] { "News.Controllers.RawData" }
   );


    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );

}

Upvotes: 9

Lucero
Lucero

Reputation: 60190

Switch the route registration, the first route to match is taken which is not the RawData one. Since the route is determined before the controller is resolved, it doesn't make a difference that the default route leads to a 404 while the RawData route would not.

You may want to install the RouteDebugger package (available through nuget) to see what goes on with routes.

Upvotes: 0

Related Questions