开发者

How to name/divide controllers in MVC3 application

At the moment I have 3 controllers: Home, Summary and Detail However, each has only one action: Index, Display and Display respectively.

This smell bad to me.

I was hoping to use the MapRoute to allow:

myapp/Home
myapp/Summary/prop1/prop2
myapp/Detail/prop1/prop2/prop3

instead of

myapp/Home
myapp/Summary/Display/prop1/prop2
myapp/Detail/Display/prop1/prop2/prop3

and thereby miss out the "Display" part...but again, this doesn't s开发者_JS百科mell right. Although it works, it means manually adding links instead of using Html.ActionLink(...)

Would it be better to have Home/Index, Home/Summary and Home/Detail all in one controller?

I was hoping to provide a simple URL structure so users who know what they are doing could simply type it in as above...the "Home" part seems wasted?


I agree with @Tim that you should use a single controller. A controller is a logical grouping of actions; for example the CRUD operations for Foo. NerdDinner is a good example of this.

I disagree with the routes. You can do whatever you want with the routing; but it should be meaningful to the user. You probably just have a single catchall route similar to the one below.

 routes.MapRoute("Default",      //RouteName
   "{controller}/{action}/{id}", //RouteUrl
   new {                         //RouteDefaults
       controller = "Home", 
       action = "Index", 
       id = UrlParameter.Optional}
 )

You can have the routes you want by using a single controller.

Your desired urls:

myapp/Home
myapp/Summary/prop1/prop2
myapp/Detail/prop1/prop2/prop3

The controller setup:

public class HomeController : Controller 
{
     public ActionResult Index() { ... }

     public ActionResult Summary() { ... }

     public ActionResult Details() { ... } 
}

The routing setup:

 routes.MapRoute("Home-Index",      //RouteName
   "myapp/Home", //RouteUrl
   new {                         //RouteDefaults
       controller = "Home", 
       action = "Index"});

 routes.MapRoute("Home-Summary",      //RouteName
   "myapp/Summary/prop1/prop2", //RouteUrl
   new {                         //RouteDefaults
       controller = "Home", 
       action = "Summary",
       prop1 = UrlParameter.Optional,
       prop2 = UrlParameter.Optional});

 routes.MapRoute("Default",      //RouteName
   "{controller}/{action}/{id}", //RouteUrl
   new {                         //RouteDefaults
       controller = "Home", 
       action = "Index", 
       id = UrlParameter.Optional}
 )

Now there are a few important things to note:

  1. Routing works like a switch statement, the first url that matches is the one it will use, that's why you have the catchall as the last one.

  2. The url defined in your map route can be whatever you want. It doesn't have to match with the controller, because once you remove the placeholders ({controller}, etc) it uses the default for navigation. So the Home-Index route url could be myapp/foo/bar/baz and id would still take you to the Home index action.

  3. The placeholders work automagically. I have not been able to find a good resource explaining how the default route works.

Hope this helps.


Not sure if I totally get your question, but what about creating a base controller class that inherits from Controller, and have your shared actions there instead? That way you don't need to repeat yourself as much.


You need only one controller and inside it multiple actions..

public class HomeController : Controller {

 public ActionResult Index()
    {

}

 public ActionResult Summary()
    {

}

 public ActionResult Details()
    {

} }

In the action link

@Html.ActionLink("Details", "Details","Home");

that's enough, no need to add multiple controller..

Hope these helps..


You can pretty much go down any route you want when it comes to this, all just depends on what you want to achieve.

You can stick all the actions in the Home controller so your routes would be

myapp/Home
myapp/Home/Summary/prop1/prop2 
myapp/Home/Details/prop1/prop2/prop3 

In this option you have 1 controller, 3 actions, with 2 additional routes to handle the URls

It depends on what the summary and details are of though? Like if it is the summary of an order, i would prefer

myapp/Orders/Summary/prop1/prop2
myapp/Orders/Details/prop1/prop2/prop3

In this you would have your Home controller and the Index action, then an Orders controller with two actions. I would say that Summary and Details would generally suggest that you are displaying something anyway, so you would not need the "Display" part as you have in your suggestions.


If you want your URLS to be

myapp/Home 
myapp/Summary/prop1/prop2  
myapp/Detail/prop1/prop2/prop3

Then you make 3 controllers

HomeController SummaryController DetailController

Each of these will have 1 Action

public ActionResult Index() {

}

For the SUmmary and Detail controller you will just pass in some extra paramaters to the Index action

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜