Search code examples
c#asp.net-coremodel-view-controllerasp.net-core-mvc

Get specific view in custom directory .NET Core


So I've already done it, but is there any other way without '.cshtml' extension ?

https://i.sstatic.net/ENehE.png

      [HttpGet("erm/nama-project/dashboard")]
        public IActionResult Index()
        {
            ViewBag.Current = "Dashboard";
            return View("/Views/Nama-Project/index.cshtml");
        }

Solution

  • If you only want to remove cshtml extension while specifying the view name, you can create a base controller and override View method as shown below:

    public override ViewResult View(string nameWithoutExtension)
    {
       return base.View($"{nameWithoutExtension}.cshtml")
    }