1

I have an area in my ASP.NET MVC application that needs to make use of partial views stored in the main application.

The code in question is being migrated from the main application into an area for organization, so I need to update the helper tags for the partial views.

Currently my tags look like this:

@await Html.PartialAsync("../Shared/Partials/_details.cshtml")

Of course, this fails in an area, since this helper only begins searching at the Areas/MyArea/ folder. I've tried adding additional ../ to the beginning of the address, but that doesn't change anything. How can I reconnect my partial views to this area?

2
  • 1
    Have you tried "~/Shared/Partials/_details.cshtml"? Commented Jun 13, 2022 at 19:35
  • @Jasen That did it, though I had to start from the bottom: "~/Views/Shared/Partials/_details.cshtml. Pls post it as an answer so I can give you credit. Commented Jun 13, 2022 at 19:42

1 Answer 1

0

You need to reference the app root.

The following example references a partial view from the app root. Paths that start with a tilde-slash (~/) or a slash (/) refer to the app root:

Partial Tag Helper:

<partial name="~/Pages/Folder/_PartialName.cshtml" />
<partial name="/Pages/Folder/_PartialName.cshtml" />

Asynchronous HTML Helper:

@await Html.PartialAsync("~/Pages/Folder/_PartialName.cshtml")
@await Html.PartialAsync("/Pages/Folder/_PartialName.cshtml")

https://learn.microsoft.com/en-us/aspnet/core/mvc/views/partial?view=aspnetcore-6.0

See also: significance of tilde sign

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.