NullReferenceException when I try to add a new ApiController in .NET Core - asp.net-core

I see this error message:
When I try to scaffold a controller for and empty API Controller by following these steps:
Right-click on folder
> Add
> Controller...
> Add Scaffold -> API Controller - Empty
> Click Add
Specifications:
Windows 10 Enterprise
Visual Studio Professional 2017 15.6.4
Microsoft .NET Framework 4.7.02556
.NET Core 2.0
Web API ASP.NET Core Web Application
---Edit---
I can't replicate the problem now. One of the suggestions given by #chriss-pratt must have worked (Thank You!) While doing a tutorial, I came across the section to add scaffold tooling and perform initial migration. In case you experience this and the below doesn't work, this part of the tutorial my be promising (if you decide to stick with scaffolding that is).

ASP.NET Core doesn't strictly have an ApiController class. ASP.NET Core doesn't have separate MVC and Web Api components. Everything is just Controller. ApiController is available through a separate "shim" NuGet package, but that's only to make migrating old ASP.NET Web Api code easier. You shouldn't be using it for any new development.
That said, the problem you're having here is with the scaffold. It might be due to the fact that you're trying to scaffold ApiController rather than just Controller. You haven't give any information about what version of Visual Studio you're using or what version of ASP.NET Core.
Try the simple stuff like closing and restarting Visual Studio. If it persists, you can try repairing Visual Studio. Hit the windows key and begin typing "Visual Studio Installer". Open that when it shows up. Below your installed copy of Visual Studio in the list, there will be a drop down menu that looks like three dots stacked on top of each other. There's an option there to "Repair". Start the process and go grab a cup of coffee. It essentially reinstalls Visual Studio, so it'll likely take a while.
All that said, honestly, your best bet is to just don't worry about it. Scaffolding is all but useless anyways. Especially with ASP.NET Core, all you get is a class, which you can easily create yourself. A controller is merely a class that inherits from Controller. Add a new class in your Controllers directory, name it WhateverController and then add : Controller after the name in the code. Then, just start adding your actions and such as normal.

Related

VS 2019 Custom ASP.NET Core project templates

I'm trying to figure out how (or even if) I can create a custom Visual Studio project template that hooks into the existing ASP.NET Core Web Application template available in Visual Studio 2019?
What I want to do is something similar to madskristensens ASP.NET Core Template Pack (GitHub source code). However instead of VS 2017 I want to do this for VS 2019's revamped "New Project Dialog" window. So imagine adding an additional ASP.NET Core web application template in the place highlighted below.
I am able to create new project templates (both for Visual Studio and for dotnet new) but nothing has worked thus far. Has anyone been able to extend VS 2019 in this way, or was this taken away after VS 2017?
After doing some more investigation between both VS 2017 and 2019 (Community) I was able to successfully figure this out! Man oh man do we need some docs on this because this felt like a doozy!
I was able to use the ASP.NET Core Template pack as a building block for putting something together. For those who don't know, the extensions are designed to add templates to Visual Studio using .nupkg file(s) embedded in the extension. The use of the .nupkg files is similar to how dotnet new works with custom templates. I've got a working prototype on GitHub that supports both Visual Studio 2017 and 2019.
Now if you're like me, you may look at either madskristensens or my project and ask "How in the heck does this work!?" Great question! Here are the details I have the time to fill out right now:
Build out a custom template (or templates) and place them into *.nupkg file(s)
In my sample, my SampleTemplates project contains three different project templates. I generate the .nupkg with dotnet pack
A vs-2017.3.host.json file is required in the .template.config/ folder so that the ASP.NET Web Application wizard can display the template.
At least 1 or more other requirements need to be fulfilled to be displayed in the ASP.NET Web Application wizard, but I haven't yet figured those out, as adding the file to a console app template doesn't cause it to appear. I want to try and figure this out, although I'd love help if anyone already knows!
The template.json needs a Framework symbol to define the list of .NET Core framework targets that are supported by the template.
In doing some testing, it didn't look like the Framework symbol didn't substitute into the .csproj, so that is why my example also includes a TargetFrameworkOverride symbol that the Framework symbol replaces.
Place the .nupkg file(s) into the root of a VSIX extension project and make sure to set the "Include in VSIX" flag to True. I emphasize root because originally I had the NuGet package landing in a build\ folder in my extension and my templates weren't being picked up.
I'll try to put together a README in my example project to provide a better outline of all of the details required to accomplish this. Fingers crossed this helps someone out (or at least help me out in the future when I inevitably forget how I accomplished this)!
Update
As of September 2nd, 2020, the Visual Studio team released an experimental feature to include dotnet new templates within Visual Studio. I have done some exceptionally minimal testing with it, and it does appear to work, but it's not perfect. For example, I created a dotnet new template that scaffolds multiple .csproj files and .sln file, and the output didn't quite match my solution.
This is the Visual Studio blog post describing the announcement: https://devblogs.microsoft.com/dotnet/net-cli-templates-in-visual-studio/

How to create .razor files

I've created a Blazor project using .net core 3.0 preview5 and VS2019 Enterprise. I want to create .razor files, but all of my options have .cshtml extensions.
When I'm creating my project, I'm using asp.net core 3.0 and Blazor server hosted options
In VS 2019 16.3.0, adding "Razor Component" is what worked for me.
What works is:
Add, Add Class
and then change .cs to .razor before clicking [Add]
And immediately delete all the C# stuff of course.
Or you can copy/paste/rename any existing blazor component, like index.razor.
Make sure you've got the blazor extension installed.
This should then allow you to click on the Pages Directory and at the top there's an Add Razor Page option.
But I've got to admit, I tend to find it quicker to just select an existing page in the solution explorer and Ctrl-C Ctrl-V and rename it!
Note: At time of writing this is using the preview branch of Visual Studio 2019 and frameworks.
Right click on Pages-> Add->New Item...-> Razor Component

Is it possible to change .cshtml View and see the changes instantly using .NET Core?

Context
In my .NET Framework 4.x ASP.NET MVC projects, when using the Visual Studio IDE, it was possible to edit a .cshtml view, save, then press ctrl+F5 in the browser and see the change immediately.
This seems to be no longer work in ASP.NET Core applications (using Visual Studio 2019 and .NET Core 3 Preview 5).
Question
Is this feature missing in ASP.NET Core? Is this a preview issue? Or am I missing something?
This is something that is no longer enabled by default as of ASP.NET Core 3, but it can be re-enabled as documented here:
Runtime compilation is enabled using the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package. To enable runtime compilation, apps must:
Install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package.
Update the project's Startup.ConfigureServices method to include a call to AddRazorRuntimeCompilation:
services
.AddControllersWithViews()
.AddRazorRuntimeCompilation();
I've marked Kirk's answer as accepted, but maybe this experience could be useful also.
My goal was a "quick edit (cshtml/css/js/ts) then see" iteration.
No need to add and mod anything... I discovered that .NET Core (3) and VS 2019 is so much faster, so in case if we do not want to debug in VS (which is the scenario in most cases when we are changing cshtml/css/js/ts) there is really great iteration:
Press ctrl+f5 to launch your web app (no debug) in browser
See, test (debug js/ts in Chrome)
Edit your cshtml/css/js/ts
Press ctrl+shift+b to build (fast!)
Switch to your browser and press ctrl+f5 to refresh your page (fast!)
Goto 2

Can I use Nuget to install MVC 4 into Webforms, or is it better to merge MVC config into Webforms, for incremental conversion

Times have changed--now we have Visual Studio 2012, MVC 4, Nuget--and it seems like the procedures in the Hanselman blog and the more recent Channel 9 tutorial could be enhanced. Both Hanselman and Kane recommend using DIFF tools to merge MVC functionality into the Web Forms Project (to endow the webform's .csproj and web.config etc with MVC). However now that Nuget manages packages, I wonder if it would be easier to merely install MVC 4 into the Web Forms project using Nuget; while of course following the procedures in the blog and the channel 9 demo for incrementally upgrading the Webforms project, including dealing with aspx login controls that postback, ascx user controls, and changing base classes Page to ViewPage and MasterPage to ViewMasterPage.
Has anyone tried to use Nuget to endow a Webforms project with MVC 4, and if so, would you recommend that technique?
UPDATE
I am following the Kane method which is an ordered set of guidelines (like modifying a complicated recipe in cooking) and am using NuGet to avoid some tedium. The guideline requires you make a dummy MVC project and compare it to your ASP.NET project to see what you need to add to your original project. You can add stuff using NuGet GUI, but that gets you latest stable assemblies versions which may lead to the dark insane demon of incompatibility. Instead use the NuGet console command line to get the compatible versions listed in the dummy MVC project's packages.config as explained in the following: How to install an older version of package via NuGet?

Make Visual Studio 'Add Controller' use Repository Pattern

In Visual Studio 2012 with an ASP.net MVC 4 project type, when you add a controller you can make it generate CRUD pages for a selected entity. Is there anyway to get it to use the repository pattern in the controllers as you can when you use the Package Manager to do the same task?
At the moment it news up the data context in the controller.
Use nuget to install the mvcscaffolding package - latest version is 1.0.9.
Now in the list of templates you have also "MvcScaffolding: Controller with read/write action and views, using repositories".