wayfarer
wayfarer

Reputation: 790

MVC view loads intermittently

I'm in total newb test and learn mode on MVC.

I built a TestController and a Test1 view, and changed RouteConfig to default = Test, Index. That works, however...

If I break on the TestController Index ActionResult line:

return View("~/Views/Test/Test1.cshtml");

and then continue, it displays my Test1 view page (which is a new, plain view, no layout, that says "Yow!" inside a div)

If I then remove the break, modify the Test1 view page (like add one character to "Yow!") and then rerun without the break, I get a 404 error.

If I reset the break and rerun, hit the break and continue, it shows the updated Test1 view.

That happens consistently.

Why would making a small modification to the Test1 view cause the localhost to not find the view? And why would doing a break and continue cause it to find the view?

Upvotes: 1

Views: 87

Answers (1)

Daniel J.G.
Daniel J.G.

Reputation: 34992

If you go to the properties of your ASP MVC project (right click project, select properties) on the Web tab there is an Start Action section. By default, Current Page is selected, which means that if you start debugging with that view currently opened, VS will try to navigate to the path of that view.

Let me explain that a bit:

  • Let's say you create a new MVC project with a Home controller and an Index view, and the typical /controller/action/id route. If you have "Current Page" selected as the option for "Start Action", and start debugging with the Index view opened, VS will try to open a URL like http://localhost:59774/Views/Home/Index.cshtml

  • But as you know, that URL won't work as you need to conform to the
    routes defined in your MVC project. For example
    http://localhost:59774/Home/Index or http://localhost:59774/ would be valid Urls for the index action in the home controller

  • That's why when you start or restart debugging with a view opened you get a 404. However when you have a controller opened, there is no Current Page and VS will try to navigate to the root of the application (like http://localhost:59774/)

You just need to change the Start Action option of your project, select Specific Page instead and leave it blank. That will result in the root being opened, same as when you start debugging from a controller.

Also, bear in mind that you can do those kind of changes to the views without the need to recompile or restart debugging.

Upvotes: 1

Related Questions