Search code examples
t4asp.net-mvc-areasasp.net-mvc-scaffolding

How do I get the Area (or the path) of where a View is being scaffolded?


PROBLEM:

I am customizing the MVC 5 templates for scaffolding views.

I need to be able to get the area name for the area where the view is being created.

EG, I want to get be able to set the value for the variable areaName for the Url Action params:

Url.Action("Index", "<#= ViewDataTypeShortName#>s", new { area = "<#= areaName #>", page<#= ViewDataTypeShortName#>s = x }), ViewContext, NormalizePath("~/Areas/<#= areaName #>/Views/<#= ViewDataTypeShortName#>s/"))

In a controller, I can do:

<#   
   var areaName = GeneratedTextTransformation.AreaName;
#>

But in the View T4 template this is not available.

So how would I get the name of the area where I am creating the view?

EDIT:

Because MVC uses convention over configuration, I could get the area if I had a way to get the path of the View file that is being created (as in:

/Areas/[Area Name]/Views/

So the question reduces to:

QUESTION:

How do I get the path of where a View is being created by the T4 ASP.NET MVC scaffolding?


Solution

  • I have solved this by using the AreaName property (available in T4 template for controller) in ViewBag.AreaName and reading this in the view.

    NOT pretty, but it will work.

    However, I would much sooner know how to access the Area the files are being created in in my T4 code. See the edit to my question.