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).
_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
To create a partial view, right-click on view -> shared folder and select Add -> View option. In this way we can add a partial view. It is not mandatory to create a partial view in a shared folder but a partial view is mostly used as a reusable component, it is a good practice to put it in the "shared" folder.
Thank you for answer but there is no multiple partial.
Partial views shouldn't be used to maintain common layout elements. Common layout elements should be specified in _Layout. cshtml files. Don't use a partial view where complex rendering logic or code execution is required to render the markup.
You can specify the area (or lack of one) in the ActionLink()
method:
Html.ActionLink(@Messages.Logout, "Logout", "Account", new { Area = "" }, new{})
This will ensure the link does not resolve to a URL within the current area.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With