User1686398519 posted
Hi mayukh_sen.
You can achieve your needs by modifying
_ViewStart.cshtml. Below I will explain how to achieve it.
- When you create a View in Area, a file called _ViewStart.cshtml is automatically created. If you haven't modified it, by default, it looks like this.
- I modified the _ViewStart.cshtml file of
each Area to look like this.
- I created a partial view called _sharedPartialView.cshtml in the "Views/Shared" directory and used it in the
_Layout.cshtml file.
- Note:
- For testing, I created a Model called Test in the Models folder which is under the root directory.
- Because there are Controller and View with the same name in my project, I also modified my Route configuration.
- I created two Areas,since they are basically the same, the following example only shows one of them.
Model
public class Test
{
public int Id { get; set; }
public string content { get; set; }
}
_sharedPartialView.cshtml(In the
Views/Shared directory)
<p>This is _sharedPartialView</p>
@model WebApplication13.Models.Test
@Html.TextBoxFor(m => m.Id)
@Html.TextBoxFor(m => m.content)
_Layout.cshtml(In the
Views/Shared directory)
<div class="container body-content">
@RenderBody()
@Html.Partial("~/Views/Shared/_sharedPartialView.cshtml")
<hr />
<footer>
<p>© @DateTime.Now.Year - My ASP.NET Application</p>
</footer>
</div>
RouteConfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "Webapplication13.Controllers" }
);
}
_ViewStart.cshtml(In the
Areas/Area1/Views directory)
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
Area1AreaRegistration
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Area1_default",
"Area1/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new string[] { "Webapplication13.Areas.Area1.Controllers" }
);
}
Here is the result.

Best Regards,
YihuiSun