Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 484
Lamloumi Afif Avatar asked Nov 18 '25 17:11

Lamloumi Afif


1 Answers

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 }
    );
}
like image 151
Vogel612 Avatar answered Nov 21 '25 06:11

Vogel612



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!