重用控制器 [英] Reusing controllers

查看:112
本文介绍了重用控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能重新使用控制器的ASP MVC?如果又如何?

Is it possible to re-use controllers in ASP MVC? And if so how?

也许再使用不正确的字。这种情况是我有一个菜单和子菜单的导航栏,如下图所示(实际上还有另外一个导航栏显示什么) - 我知道的配色方案需要一些工作

Perhaps re-use isn't the right word. The situation is I have a menu and sub menu navigation bars as shown below (actually there is another nav bar what is shown)- I know the colour scheme needs some work

嵌套导航

的上部杆从数据库填充,所以有可能是大于或小于3的计划。

The upper bar is populated from a database, so there could be more or less than 3 plans.

的下杆总是具有相同的三个条目。对于每个这些项的意见是,无论相同,其中被选择的计划,尽管它们是彼此不同的。显然,在其中的数据是不同的(不同表中填写)。

The lower bar always has the same three entries. The views for each of these entries are the same regardless of which plan is selected, though they are different from each other. Obviously the data within them is different (populated from different tables).

这是 A计划 - >建议的要点的看法是一样的 B计划 - >建议的要点视图。
A计划 - >建议点观点是不一样的 A计划 - >接受点视图

That is Plan A -> Suggested Points view is the same as Plan B -> Suggested Points view. But Plan A -> Suggested Points view is not same as Plan A -> Accepted Points view

为了与我打算用部分景色的意见要做到这一点,所以相同的视图文件可以被重新使用。

In order to do this with the views I intend to use partial views, so the same view files can be re-used.

不过,我该怎么办的控制器相同呢?

However, how can I do the equivalent for the controllers?

我想如果为URL路径,例如:
    /玻璃体/ SuggestedPoints
    / PlanB / SuggestedPoints

What I would like if for url paths such as: /PlanA/SuggestedPoints /PlanB/SuggestedPoints

在我看来,我只是想在计划链接来设置一个变量,它告诉分数意见的数据库,他们应该挂钩到。这可能是认为它走错了路,我怀疑是与上面的URL路径的建议是不相容的。

To my mind I just want the Plan links to set a variable that tells the Points views which database they should hook up to. Which may be the wrong way to think of it and I suspect is incompatible with the url path suggestion above.

推荐答案

建议的方法

我建议最好是在路线包括一个控制器名称,这样你就不会在你的应用程序的其他控制器的冲突得到这么容易。

I would suggest it is better to include a controller name in the route, that way you won't get conflicts so easily with other controllers in your app.

您可以修改您的RouteConfig.cs文件,并映射一个新的途径。之前,请务必默认一添加自定义路径

You can modify your RouteConfig.cs file and map a new route. Make sure to add the custom route before the "Default" one.

事情是这样的:

routes.MapRoute(
    "Plans",
    "Plans/{planName}/{action}",
    new { controller = "Plans", action = "Index" }
);

// Default route here.

然后,你将有一个名为计划控制器<​​/ code>每有一个名为 planName 参数,使您的操作有计划,工作确定...

Then you would have a controller called Plans with each of your actions having a parameter called planName that lets you identify with plan to work with...

public class PlansController : Controller
{
    public ActionResult SuggestedPoints(string planName)
    {
        // create your view here, using the planName to get the correct data.
    }

    public ActionResult AcceptedPoints(string planName)
    {
        // create your view here, using the planName to get the correct data.
    }

    // etc.
}

此方法将允许的URL的格式如下:

This method will allow URL's in the following format:

/计划/玻璃体/ SuggestedPoints /计划/玻璃体/ SuggestedPoints /图/ PlanB / AcceptedPoints /计划/ PlanB / AcceptedPoints

请注意:如果你的计划是在数据库中,它可能是更有益使用一个ID为计划,但该URL的看起来不那么友好,这样是给你

NOTE: If your plans are in your database, it may be more beneficial to use an ID for the plan, but the URL's would look less friendly so that is up to you.

最后,当你想在视图中创建你的链接,你可以使用以下内容:

Finally, when you want to create your links in your view, you can use the following:

@Html.RouteLink("link text", "SuggestedPoints", new { controller = "Plans", planName = "PlanA" })


您确切的请求

如果你完全必须:使用你建议的URL格式,那么你可以做需要为每个操作路径以下,但要注意,你将需要依靠行动的唯一性名称,以确保它们不会与其他控制器冲突...

If you absolutely must use the URL formats you suggested, then you can do the following which requires a route for each action, but be wary that you will need to rely on the uniqueness of the Action names to ensure they don't conflict with other controllers...

routes.MapRoute(
    "SuggestedPoints",
    "{planName}/SuggestedPoints",
    new { controller = "Plans", action = "SuggestedPoints" }
);

routes.MapRoute(
    "AcceptedPoints",
    "{planName}/AcceptedPoints",
    new { controller = "Plans", action = "AcceptedPoints" }
);

routes.MapRoute(
    "RejectedPoints",
    "{planName}/RejectedPoints",
    new { controller = "Plans", action = "RejectedPoints" }
);

// Default route here.

在这种情况下,控制器将保持相同我的上述的第一个建议。这将允许URL就像如下:

In this instance, the controller will remain the same as my first suggestion above. Which will allows URL's like as follows:

/玻璃体/ SuggestedPoints /玻璃体/ SuggestedPoints / PlanB / AcceptedPoints / PlanB / AcceptedPoints

这篇关于重用控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆