如何在 MVC3 应用程序中命名/划分控制器

发布于 2024-12-04 11:17:55 字数 534 浏览 1 评论 0原文

目前我有 3 个控制器:主页、摘要和详细信息 然而,每个都只有一个操作:分别是索引、显示和显示。

这味道对我来说很难闻。

我希望使用 MapRoute 来允许:

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

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

不是从而错过“显示”部分......但同样,这闻起来不对。虽然它可以工作,但这意味着手动添加链接而不是使用 Html.ActionLink(...)

将 Home/Index、Home/Summary 和 Home/Detail 全部放在一个控制器中会更好吗?

我希望提供一个简单的 URL 结构,以便知道自己在做什么的用户可以简单地输入上面的内容......“主页”部分似乎浪费了?

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 smell 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

在风中等你 2024-12-11 11:17:55

我同意@Tim 的观点,你应该使用单个控制器。控制器是动作的逻辑分组;例如 Foo 的 CRUD 操作。 NerdDinner 就是一个很好的例子。

我不同意路线。你可以对路由做任何你想做的事情;但它应该对用户有意义。您可能只有一条类似于下面的包罗万象的路线。

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

您可以使用单个控制器获得您想要的路线。

您想要的网址:

myapp/主页
myapp/Summary/prop1/prop2
myapp/Detail/prop1/prop2/prop3

控制器设置:

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

     public ActionResult Summary() { ... }

     public ActionResult Details() { ... } 
}

路由设置:

 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}
 )

现在有一些重要的事情需要注意:

  1. 路由的工作方式类似于 switch 语句,第一个匹配的 url 是
    它将使用的那个,这就是为什么你将包罗万象的东西作为最后一个
    一个。

  2. 地图路线中定义的 url 可以是您想要的任何内容。它
    不必与控制器匹配,因为一旦你删除
    它使用默认值的占位符({controller}等)
    导航。所以 Home-Index 路由 url 可能是 myapp/foo/bar/baz
    并且 id 仍会将您带到主页索引操作。

  3. 占位符会自动工作。我一直无法找到解释默认路由如何工作的好资源。

希望这有帮助。

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.

与酒说心事 2024-12-11 11:17:55

不确定我是否完全明白你的问题,但是创建一个继承自 Controller 的基本控制器类,并在其中包含你的共享操作怎么样?这样你就不需要重复太多。

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.

囍孤女 2024-12-11 11:17:55

您只需要一个控制器,并在其中执行多个操作。

public class HomeController : Controller
{

 public ActionResult Index()
    {

}

 public ActionResult Summary()
    {

}

 public ActionResult Details()
    {

}
@

在操作链接中

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

这就足够了,不需要添加多个控制器..

希望这些有帮助..

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..

世界等同你 2024-12-11 11:17:55

在这方面,您几乎可以走任何您想要的路线,一切都取决于您想要实现的目标。

您可以将所有操作粘贴在主控制器中,这样您的路线将是

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

在此选项中,您有 1 个控制器,3 个操作,还有 2 个附加路线来处理 UR,

但这取决于摘要和详细信息是什么?就像如果它是订单摘要一样,我更希望

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

在其中您拥有主控制器和索引操作,然后是具有两个操作的订单控制器。我想说的是,摘要和详细信息通常会建议您无论如何都要显示某些内容,因此您不需要像建议中那样的“显示”部分。

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.

原谅过去的我 2024-12-11 11:17:55

如果你希望你的 URLS 是

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

那么你制作 3 个控制器

HomeController
摘要控制器
DetailController

其中每一个都有 1 个 Action

公共 ActionResult Index() {

}

对于 SUmmary 和 Detail 控制器,您只需将一些额外的参数传递给 Index 操作

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文