Lamloumi Afif
Lamloumi Afif

Reputation: 9081

Getting a view by changing url in razor

I have a problem in the file RouteConfig.cs in my asp.net mvc4 application :

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

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

            routes.MapRoute(
               name: "Admin",
               url: "Admin",
               defaults: new { controller = "Home", action = "Administration" }
           );
        }

in the home controller :

public class HomeController : Controller
    {
        public ActionResult Index()
        {
         return View();
        }

        public ActionResult Administration()
        {
            return View();
        }

and added the view :

views

but when i put http://localhost:61961/Admin as url the view is not found.

Why this happens? how can i fix it?

Upvotes: 3

Views: 789

Answers (1)

Vogel612
Vogel612

Reputation: 5647

The Routing config is ordered. please move your administration-Config to the top of RegisterRoutes.

Right now you would be mapped to

Controller: admin
View: index

as your default routing catches your localhost:{port}/admin

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

    routes.MapRoute(
       name: "Admin",
       url: "Admin",
       defaults: new { controller = "Home", action = "Administration" }
    );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

Upvotes: 3

Related Questions