ASP.net MVC - 在区域之间共享部分

时间:2022-11-06 01:57:14

is there some way to share a partial razor view between areas?

有没有办法在区域之间分享部分剃刀视图?

For example a login partial, it is found if i use @Html.Partial("_LoginPartial") but the URLs Html.ActionLink generates are local to the calling area (even though the partial itself is not part of the area).

例如,登录部分,如果我使用@ Html.Partial(“_ LoginPartial”),则会发现,但Html.ActionLink生成的URL对于调用区域是本地的(即使部分本身不是该区域的一部分)。

_LoginPartial.cshtml is in /Views/Shared/_LoginPartial.cshtml
Calling view is inside /Areas/Somearea/Views

Links generated are like: http://example.com/Somearea/Account/Login
But should always be: http://example.com/Account/Login

Partial view source:

部分视图来源:

@if(Request.IsAuthenticated) {
    <text>Welcome <b>@Context.User.Identity.Name</b>!
    [ @Html.ActionLink(@Messages.Logout, "Logout", "Account") ]</text>
}
else {
    @:[ @Html.ActionLink(@Messages.Login, "Login", "Account") ]
}

Thanks

谢谢

1 个解决方案

#1


9  

You can specify the area (or lack of one) in the ActionLink() method:

您可以在ActionLink()方法中指定区域(或缺少一个区域):

Html.ActionLink(@Messages.Logout, "Logout", "Account", new { Area = "" }, new{})

This will ensure the link does not resolve to a URL within the current area.

这将确保链接无法解析为当前区域内的URL。

#1


9  

You can specify the area (or lack of one) in the ActionLink() method:

您可以在ActionLink()方法中指定区域(或缺少一个区域):

Html.ActionLink(@Messages.Logout, "Logout", "Account", new { Area = "" }, new{})

This will ensure the link does not resolve to a URL within the current area.

这将确保链接无法解析为当前区域内的URL。