Asp.net mvc routing question

standard routing works fine

mysite.com/home/about

and I even see how to configure it to make it shorter

so i can say:

mysite.com/edit/1 instead of mysite.com/home/edit/1

but how can I do this longer to handle the url like the following

mysite.com/admin/user/1// works

mysite.com/admin/user/details// does not work

mysite.com/admin/question/create// not working

Can't I just see the identifier as an action? Do I need a custom route?

I need to create new controllers for each table or I can route them through the Admin controller.

Many thanks

+3
source share
2 answers

, ,

, . , , , . , .

routes.MapRoute("AdminQuestions", // Route name
                "admin/question/{action}/{id}", // URL with parameters
                new { controller = "AdminQuestion", action = "Index" } // Parameter defaults
    );

routes.MapRoute("AdminUsers", // Route name
                "admin/user/{action}/{id}", // URL with parameters
                new { controller = "AdminUser", action = "Index" } // Parameter defaults
    );

Admin, , .

routes.MapRoute("Admin", // Route name
                "admin/{action}/{type}/{id}", // URL with parameters
                new { controller = "Admin", action = "Index" } // Parameter defaults
    );

() AdminController, :

public virtual ActionResult Create(string type, int id)
{
    switch (type)
    {
        case 'question':
            // switch/case is code smell
            break;
        case 'user':
            // switch/case is code smell
            break;
        // etc
     }
}
+2

global.asax . . ///... - , MVC. "mysite.com/admin/user/details" "admin" "" , ( )

+1

Source: https://habr.com/ru/post/1758079/


All Articles