Share via


First MVC app, can't find view

Question

Wednesday, July 10, 2013 7:56 PM

I decided to try to learn MVC. I started with a empty MVC 4 project in VS2012. I added a Controller to the Controllers folder called: "HomeController.cs" I changed the Index() method to return a ViewResult:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PartyInvites.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ViewResult Index()
        {
            return View();
        }

    }
}

Then I added a view "Index.cshtml":

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        Hello World! (from the view)
    </div>
</body>
</html>

When I run it here is the error I get:

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Home/Index.aspx ~/Views/Home/Index.ascx ~/Views/Shared/Index.aspx ~/Views/Shared/Index.ascx ~/Views/Home/Index.cshtml ~/Views/Home/Index.vbhtml ~/Views/Shared/Index.cshtml ~/Views/Shared/Index.vbhtml

 

stack trace:

[InvalidOperationException: The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Home/Index.aspx ~/Views/Home/Index.ascx ~/Views/Shared/Index.aspx ~/Views/Shared/Index.ascx ~/Views/Home/Index.cshtml ~/Views/Home/Index.vbhtml ~/Views/Shared/Index.cshtml ~/Views/Shared/Index.vbhtml]    System.Web.Mvc.ViewResult.FindView(ControllerContext context) +355958    System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +121    System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13    System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +23    System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +242    System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +21    System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177    System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +89    System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +102    System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57    System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +43    System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14    System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23    System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62    System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +57    System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23    System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62    System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +47    System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10    System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25    System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23    System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62    System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +47    System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9628700    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

 

link to solution explorer screen shot:

https://skydrive.live.com/redir?resid=8325728C0FE3EF5A!6509&authkey=!AJUFMcJ8RYbRYTQ

 

Can anyone tell me why this happening. UnlessI missed something, What its looking for is where it is supposed to be.

Thanks in advance!

 

All replies (2)

Wednesday, July 10, 2013 8:07 PM âś…Answered

Index.cshtml should be withing a folder called Home under Views folder.

try adding the view by right clicking on the Index action and try not to add views manually. 

Before you start to do with an empty project, try to go through some tutorials first to get a grasp of MVC. Please take a look at this tutorial http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4

Hope that helps.


Wednesday, July 10, 2013 8:11 PM

Hi remojr76,

Try the following in Controller

public ActionResult Index()
        {
            return View();
        }

And your Index.cshtml should be in the Home Folder in Views Folder

i.e. Views/Home/Index.cshtml

Hope this works.

Thanks,

Jatin