如何在不添加area =“”的情况下指定默认区域到每个 ActionLink

发布于 2024-08-22 23:38:32 字数 340 浏览 10 评论 0原文

我有一个基于 ASP.NET MVC2 RC2 构建的大型现有应用程序。

我的所有链接如下所示:htp//site/controller/action/id

我刚刚添加了一个名为:BigBird 的区域。

现在,当我在 BigBird 区域时,我的所有链接如下所示: htp://site/BigBird/controller/action/id

问题是这些控制器/操作都不存在于我的新区。所以我必须遍历我的应用程序中的所有操作链接并放置此路由值:area = string.empty

有什么办法可以解决这个问题吗?

I have a large existing application built on ASP.NET MVC2 RC2.

All of my links look like this: htp//site/controller/action/id

I just added an Area called: BigBird.

Now when I'm in the BigBird area, all of my links look like this: htp://site/BigBird/controller/action/id

Problem is that none of those controllers/actions exist in my new Area. So I have to go through all of my actionlinks all over my application and put this routevalue: area = string.empty

Is there any way around this?

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

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

发布评论

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

评论(2

拥抱没勇气 2024-08-29 23:38:32

如果您使用标准 MVC 方法(除了可能覆盖它们来调用您自己的版本),但如果您使用 ActionLink 或其他通用方法,我不知道如何解决这个问题MvcFutures 库中提供的方法就可以。

MvcFutures 方法调用 ExpressionHelper.GetRouteValuesFromExpression(),它会在控制器上查找 ActionLinkAreaAttribute 以确定区域。因此,您可以在主“区​​域”中装饰控制器,如下所示:

[ActionLinkArea("")]
[HandleError]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

应使用标准语法正确生成操作链接:

<%= Html.ActionLink<HomeController>(c => c.Index(), "Home") %>

I don't know of away around it if you are using the standard MVC methods (other than maybe overriding them to call your own version), but if you are using the ActionLink<TController> or other generic methods provided in the MvcFutures lib then you can.

The MvcFutures methods call ExpressionHelper.GetRouteValuesFromExpression(), which looks for an ActionLinkAreaAttribute on the controller to determine the area. So you can decorate your controllers in your main "area" as follows:

[ActionLinkArea("")]
[HandleError]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

The action links should be generated correctly using the standard syntax:

<%= Html.ActionLink<HomeController>(c => c.Index(), "Home") %>
绝對不後悔。 2024-08-29 23:38:32

您可以做两件事之一。您可以将控制器/操作移动/复制到适当的区域,或者为新区域编写一些新控制器(这是我推荐的方法),或者您可以编写一个自定义路由,强制将新区域转移到根(我将其不推荐,因为它违背了拥有区域的全部目的):

routes.MapRoute(
    "BigBird_Override",                                             
    "BigBird/{controller}/{action}/{id}",                          
    new { area = String.Empty }
);

You can do one of two things. You can either move/copy your controllers/actions into the proper area or write some new controllers for the new area (which is the approach I recommend), or you can write a custom route that forces the new area to the root (which I don't recommend, as it defeats the whole purpose of having areas):

routes.MapRoute(
    "BigBird_Override",                                             
    "BigBird/{controller}/{action}/{id}",                          
    new { area = String.Empty }
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文