Set default area - Avoiding `, new {area = “”}` on each link on the site

偶尔善良 提交于 2019-12-03 06:39:55

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

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

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.

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") %>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!