1

I am expecting that a new controller for a Web Api (ie- no UI, no views etc), would be created by VS2017 to inherit from

AspNetCore.Mvc.ControllerBase (in Microsoft.AspNetCore.Mvc.Core.dll)

instead of from:

AspNetCore.Mvc.Controller (which is in the Microsoft.AspNetCore.Mvc.ViewFeatures.dll - and contains view support)

Example controller generated by VS:

namespace WebApi1.Controllers
{
    [Route("api/[controller]")]
    public class ValuesController : Controller
    {   
        // ....

Can someone help me understand if I'm missing something here?

If not, does anyone have any ideas on how to prevent use of Controller in a WebApi only project?

I had thought of a hacky method of implementing a base class in my WebApi1.Controllers namespace called Controller that effectively intercepts a WebApi controller that uses Controller over ControllerBase, but perhaps a filter might be better?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Peter
  • 971
  • 11
  • 12
  • 1
    I assume you mean when you create a new controller using the VS wizard? Why can't you just change the code to use `ControllerBase`? – Charleh Dec 07 '17 at 10:37
  • Well yes I can and usually would remember to, will all other developers? Will I always remember? ... – Peter Dec 07 '17 at 10:38
  • 2
    I hope this helps [link](https://github.com/aspnet/Mvc/issues/4785) – ChiragMM Dec 07 '17 at 10:52
  • 2
    I'd imagine it's just an oversight in the VS deploy since the .NET core stuff is moving quite quickly. https://stackoverflow.com/questions/8745688/how-can-i-change-the-default-visual-studio-c-sharp-new-class-file-template - I'd just edit your templates for now and maybe raise the issue on the GH repo – Charleh Dec 07 '17 at 10:53
  • 1
    Thanks @Charleh & @chiragMM - for now I will add this class in my controller's namespace with suitable comment, which should cause a compiler error for scaffolded types: `internal abstract class Controller { Controller() { } }` – Peter Dec 07 '17 at 11:59

0 Answers0