在站点的每个链接上设置默认区域 - 避免`,new {area =“”}`

时间:2022-09-30 07:07:11

This code is inside the master page:

此代码位于母版页内:

<li><a href="<%=Url.Action("Action", "Controller") %>">Main site link</a></li>
<li><a href="<%=Url.Action("AreaAction", "AreaController", new {area = "Area"}) %>">Area link</a></li>

All the links works good till I'm going to the Area Link. When I go there all the routes of the main area don't work.

所有的链接都很好,直到我去区域链接。当我去那里时,主要区域的所有路线都不起作用。

To fix that I can use this:

为了解决这个问题,我可以使用它:

<li><a href="<%=Url.Action("Action", "Controller", new {area = ""}) %>">Main site link</a></li>

My question is, is there a way to avoid , new {area = ""} on every link in the to the main site?

我的问题是,有没有办法避免在主站点的每个链接上使用新的{area =“”}?

Its very annoying to have this on every link on the site.

在网站的每个链接上都有这个非常烦人。

2 个解决方案

#1


10  

Url actions are relative to the location of the link. So new {area = ""} is not telling the Url.Action call that there is no area, it's telling it to use the root area. If you omit new {area = ""} from the Url.Action call it will try to create a url for the specified action within the specified controller within the current area (the "Area" are in your case).

网址操作与链接的位置有关。所以新的{area =“”}没有告诉Url.Action调用没有区域,它告诉它使用根区域。如果从Url.Action调用中省略新的{area =“”},它将尝试为当前区域内的指定控制器中的指定操作创建一个url(在您的情况下为“Area”)。

Therefore it is unavoidable if you want to link from a subarea to the root area.

因此,如果要从子区域链接到根区域,则不可避免。

#2


3  

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

如果你使用标准的MVC方法(除了可能会覆盖它们来调用你自己的版本),我仍然不知道如何使用它,但是如果你使用的是ActionLink 或MvcFutures lib中提供的其他泛型方法然后你可以。

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:

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

[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") %>

#1


10  

Url actions are relative to the location of the link. So new {area = ""} is not telling the Url.Action call that there is no area, it's telling it to use the root area. If you omit new {area = ""} from the Url.Action call it will try to create a url for the specified action within the specified controller within the current area (the "Area" are in your case).

网址操作与链接的位置有关。所以新的{area =“”}没有告诉Url.Action调用没有区域,它告诉它使用根区域。如果从Url.Action调用中省略新的{area =“”},它将尝试为当前区域内的指定控制器中的指定操作创建一个url(在您的情况下为“Area”)。

Therefore it is unavoidable if you want to link from a subarea to the root area.

因此,如果要从子区域链接到根区域,则不可避免。

#2


3  

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

如果你使用标准的MVC方法(除了可能会覆盖它们来调用你自己的版本),我仍然不知道如何使用它,但是如果你使用的是ActionLink 或MvcFutures lib中提供的其他泛型方法然后你可以。

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:

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

[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") %>