Search code examples
asp.net-mvc-3t4scaffolding

scaffold template use cshtml


When I generate my controller and views with the below command

scaffold controller <Entity> -force -repository -DbContextType "XXX" -Area YYY

It generates .aspx (web form) pages instead of .cshtml (razor)

How can I change this default behaviour. I think when I first created a new project it asked me to select the default view engine and I picked the wrong one (webforms).

Also are there any free or cheap T4 templates for MVC 3 that generate nicer and more functional views. i.e using webgrid / jQUery etc.


Solution

  • Solution wide scaffolders configuration is stored in scaffolding.config which is located in the same folder with solution file.

    On installation stage MvcScaffolding package launches init.ps script (you can find it in <packages folder>\MvcScaffolding.<version>\tools directory). Script counts aspx, cshtml and vbhtml views and based on these numbers decideds what view scaffolder will be used. Here is a piece of this logic:

    function InferPreferredViewEngine() {
        # Assume you want Razor except if you already have some ASPX views and no Razor ones
        if ((CountSolutionFilesByExtension aspx) -eq 0) { return "razor" }
        if (((CountSolutionFilesByExtension cshtml) -gt 0) -or ((CountSolutionFilesByExtension vbhtml) -gt 0)) { return "razor" }
        return "aspx"
    }
    
    # Infer which view engine you're using based on the files in your project
    $viewScaffolder = if ([string](InferPreferredViewEngine) -eq 'aspx') { "MvcScaffolding.AspxView" } else { "MvcScaffolding.RazorView" }
    Set-DefaultScaffolder -Name View -Scaffolder $viewScaffolder -SolutionWide -DoNotOverwriteExistingSetting
    

    So you can switch view scaffolder using following commands:

    Set-DefaultScaffolder -Name View -Scaffolder "MvcScaffolding.RazorView" -SolutionWide
    Set-DefaultScaffolder -Name View -Scaffolder "MvcScaffolding.AspxView" -SolutionWide
    

    Or you can manually edit scaffolding.config file and replace value for ScaffolderName attribute in tag:

    <Default DefaultName="View" ScaffolderName="put here either MvcScaffolding.RazorView or MvcScaffolding.AspxView" />