MVC Tell one view to one use controller, and another view to use another

时间:2018-02-01 18:39:29

标签: asp.net-mvc asp.net-mvc-routing

I have an application where I don't want to use any "folders". EG

http://mydomain/Index , http://mydomain/Edit , http://mydomain/Admin , and so on....

I modified my defauly RouteConfig initially to look like this:

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

I have a Views/Home folder with all my views.

It works like a charm so whenever I enter the actionname, I don't have any issues.

If, hypothetically, I wanted to KEEP this url structure the same where I ONLY show the root domain / action-page... and I want to have SOME of these actions use one controller (HomeController.cs) and other actions (e.g. Admin) use another contoller (AdminController), is there I way I can modify my routes to say... ok.. if the action is "Admin" then use the AdminController? I have the AdminController set up with the action for "Admin" defined. I tried changing my routeconfig like this:

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

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

and then I set up a Views/Admin folder for the Admin view (and some other actions/views I want)

But it doesn't work. IF I have an "Admin" action in the home controller, it fires from the home controller. But I want the action to fire from the AdminController. Keep in mind I'm not just going to use ActionLinks to navigate to these pages so I can't just use an Actionlink and specify the controller. Some people are going to be going to links via a direct URL/bookmark.

If I wanted to keep my URL structure like this (http://mydomain/Action) I could just slap all my actions into ONE single controller and it would work, but I like to keep my code neat so that one controller handles functionality related to one set of models and another controller related to another set of models. For now it seems like I have to either (a) have one MASSIVE controller that is just going to get bigger and bigger or (b) I HAVE to have a more detailed path in there to have something to tell MVC what controller to use.

(BTW, I can't use querystrings... long story... don't ask)

MVC was designed to give people more control over layout from what I understand. I've been a web forms programmer for 13 years, but so far it seems that MVC has done this at the expense of giving you less control in other areas.

2 个答案:

答案 0 :(得分:1)

您可能需要在路线中对您的动作名称进行硬编码。

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

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

http://localhost/Admin应该转到管理员/索引视图,http://localhost/Home应该转到主页/索引视图

答案 1 :(得分:0)

一个简单的解决方法是在家庭控制器中创建一个admn动作,然后从那里重定向到Admin控制器,但我不确定你是否想要这样做,因为它可能会达到性能

public ActionResult Admin()
{ 
 return Redirect("Admin/Index");
}

我会说尝试别的,如果可以,即便如果你找不到出路,也可以使用这种解决方法