Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 875
Daveo Avatar asked Feb 05 '13 10:02

Daveo


People also ask

How to create a scaffolding template in MVC 4?

Now let’s create a basic ASP.NET MVC 4 application to understand View scaffolding Template for that Open visual studio à Go to File à Select New à Select Project. After that, you will see a new dialog for selecting your Template and Project type.

What is the scaffold templates repository?

This repository contains scaffold templates that are alternatives to the native .NET Core scaffold templates for web application with MVC and RazorPages. The scaffolding logic and tools are the same as with the ordinary .NET Core web applications.

What is scaffold?

Scaffold is a modern and fresh HTML website template built with bootstrap framework. It's awesome and creative responsive HTML5 template created for business, corporate, portfolio, startup and agency websites.

How do I override a T4 scaffold in MVC?

Index.cshtml Index.vbhtml To create project-specific templates, copy the files you want to override from the original T4 scaffold folder to a folder in the ASP.NET MVC Web project called CodeTemplates (it must have this exact name). By convention, the scaffold subsystem first looks in the MVC project’s CodeTemplates folder for a template match.


1 Answers

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" />
like image 67
Alexander Manekovskiy Avatar answered Oct 13 '22 00:10

Alexander Manekovskiy