2

When trying to create a generic .NET Core MVC controller that returns views, the views aren't found(Error message: "Cannot resolve View Details"). This is happening because, the generic controller isn't tied to a specific view. The view should be picked based on what the T is. I've seen some examples of this in ASP.NET, but I'm unable recreate those in .NET Core.

Is there a good way to solve this problem in .NET Core?

Generic controller example:

public class ControllerBase<T> : Controller where T : class
{
    private IManager<T> _manager;

    public ControllerBase( IManager<T> manager)
    {
        _manager = manager;
    }

    public async Task<IActionResult> Details(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var result = await _manager.Get(id);
        return View(result);
    }
}
Eliise S
  • 51
  • 4
  • 1
    Are your views present and placed in the right location? [Documentation on views](https://learn.microsoft.com/en-us/aspnet/core/mvc/views/overview) – Rostech Jul 25 '17 at 11:10
  • Yeah, but the problem is that there is no view for the generic controller, since it's supposed to pick the view based on what the T is. I'm just not sure how to implement that. – Eliise S Jul 25 '17 at 11:15
  • yes, I understood the real issue from your edit. – Rostech Jul 25 '17 at 11:16
  • Great! Sorry for not being clear right away. – Eliise S Jul 25 '17 at 11:17
  • 2
    You can pass view name to `View()` method: `View(typeof(T).Name, result);` – Aleks Andreev Jul 25 '17 at 11:18
  • Changing `View()` to `View(typeof(T).Name, result)` results in an error of: _InvalidOperationException: The view 'Installation' was not found. The following locations were searched: /Views/Installations/Installation.cshtml /Views/Shared/Installation.cshtml_ The name of the view is 'Index' (Method name). But thanks to this, I've solved the problem! Or, more correctly, there was no problem at all. The issue was with resharper displaying an error that wasn't actually an error. The original solution works as intended. – Eliise S Jul 25 '17 at 11:37

2 Answers2

0

As already mentioned in the comments you can can specify the view to use via

return View("viewName", result);

where viewName should be adapted to your requirements (your question suggests maybe the type name of the type argument?

There may be a problem for the ViewEngine locating your view depending on where you placed them and how your controllers are arranged. But you can get around this by either specifying the full path to the view:

return View("~/Views/path/to/your/generic/viewName.cshtml", result);

or extending the search paths in the ViewEngine as mentioned here: Can I specify a custom location to "search for views" in ASP.NET MVC?

Philipp Grathwohl
  • 2,726
  • 3
  • 27
  • 38
0

The original code actually works as intended, the issue for me lied with ReSharper reporting an issue where there actually wasn't anything wrong. My fault for missing it.

Thanks everyone for your help.

Eliise S
  • 51
  • 4