The view 'Index' or its master was not found. - asp.net-mvc

The view 'Index' or its master was not found. The following locations were searched:
~/Views/ControllerName/Index.aspx
~/Views/ControllerName/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
I got this error when using ASP.Net mvc area. The area controller action are invoked, but it seems to look for the view in the 'base' project views instead of in the area views folder.

What you need to do is set a token to your area name:
for instance:
context.MapRoute(
"SomeArea_default",
"SomeArea/{controller}/{action}/{id}",
new { controller = "SomeController", action = "Index", id = UrlParameter.Optional }
).DataTokens.Add("area", "YOURAREANAME");

This error was raised because your Controller method name is not same as the View's name.
If you right click on your controller method and select Go To View (Ctrl+M,Ctrl+G), it will either open a View (success) or complain that it couldn't find one (what you're seeing).
Corresponding Controllers and View folders name have the same names.
Corresponding Controller methods & Views pages should same have the same names.
If your method name is different than view name, return view("viewName") in the method.

Global.asax file contain the URL Route.
Default URL route like this.
"{controller}/{action}/{id}"
So,Try this.
1. Right click your controller method as below.
Example: let say we call Index() method.Right click on it.
2. Click Add View.. and give appropriate name.In this example name should be Index.
Then it will add correct View by creating with relevant folder structure.

Check the generated code at MyAreaAreaRegistration.cs and make sure that the controller parameter is set to your default controller, otherwise the controller will be called bot for some reason ASP.NET MVC won't search for the views at the area folder
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"SomeArea_default",
"SomeArea/{controller}/{action}/{id}",
new { controller = "SomeController", action = "Index", id = UrlParameter.Optional }
);
}

Where this error only occurs when deployed to a web server then the issue could be because the views are not being deployed correctly.
An example of how this can happen is if the build action for the views is set to None rather than Content.
A way to check that the views are deployed correctly is to navigate to the physical path for the site on the web server and confirm that the views are present.

The problem was that I used MvcRoute.MappUrl from MvcContrib to route the context.Routes.
It seems that MvcContrib routing mapper was uncomfortable with area routing.

You most likely did not create your own view engine.
The default view engine looks for the views in ~/Views/[Controller]/ and ~/Views/Shared/.
You need to create your own view engine to make sure the views are searched in area views folder.
Take a look this post by Phil Haack.

I had this problem today with a simple out of the box VS 2013 MVC 5 project deployed manually to my local instance of IIS on Windows 8. It turned out that the App Pool being used did not have the proper access to the application (folders, etc.). After resetting my App Pool identity, it worked fine.

right click in index() method from your controller
then click on goto view
if this action open index.cshtml?
Your problem is the IIS pool is not have permission to access the physical path of the view.
you can test it by giving permission. for example :- go to c:\inetpub\wwwroot\yourweb then right click on yourweb folder -> property ->security and add group name everyone and allow full control to your site . hope this fix your problem.

It´s still a problem on the Final release.. .when you create the Area from context menu/Add/Area, visual studio dont put the Controller inside de last argument of the MapRoute method. You need to take care of it, and in my case, I have to put it manually every time I create a new Area.

You can get this error even with all the correct MapRoutes in your area registration. Try adding this line to your controller action:
If Not ControllerContext.RouteData.DataTokens.ContainsKey("area") Then
ControllerContext.RouteData.DataTokens.Add("area", "MyAreaName")
End If

If You can get this error even with all the correct MapRoutes in your area registration and all other basic configurations are fine.
This is the situation:
I have used below mentioned code from Jquery file to post back data and then load a view from controller action method.
$.post("/Customers/ReturnRetailOnlySales", {petKey: '<%: Model.PetKey %>'});
Above jQuery code I didn't mentioned success callback function.
What was happened there is after finishing a post back scenario on action method, without routing to my expected view it came back to Jquery side and gave view not found error as above.
Then I gave a solution like below and its working without any problem.
$.post("/Customers/ReturnRetailOnlySales", {petKey: '<%: Model.PetKey %>'},
function (data) {
var url = Sys.Url.route('PetDetail', { action: "ReturnRetailOnlySalesItems", controller: "Customers",petKey: '<%: Model.PetKey %>'});
window.location = url;});
Note: I sent my request inside the success callback function to my expected views action method.Then view engine found a relevant area's view file and load correctly.

I have had this problem too; I noticed that I missed to include the view page inside the folder that's name is same with the controller.
Controller: adminController
View->Admin->view1.cshtml
(It was View->view1.cshtml)(there was no folder: Admin)

This error can also surface if your MSI installer failed to actually deploy the file.
In my case this happened because I converted the .aspx files to .cshtml files and visual studio thought these were brand new files and set the build action to none instead of content.

I got the same problem in here, and guess what.... looking at the csproj's xml' structure, I noticed the Content node (inside ItemGroup node) was as "none"... not sure why but that was the reason I was getting the same error, just edited that to "Content" as the others, and it's working.
Hope that helps

Add the following code in the Application_Start() method inside your project:
ViewEngines.Engines.Add(new RazorViewEngine());

I added viewlocationformat to RazorViewEngine and worked for me.
ViewLocationFormats = new[] {
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml",
"~/Areas/Admin/Views/{1}/{0}.cshtml",
"~/Areas/Admin/Views/Shared/{0}.cshtml"
};

Related

MVC4 - after adding new controller, corresponding view is not generated

I started learning Asp.NET MVC and got stuck pretty immediately. According to all materials I have, after I add controller in MVC project (Template - empty MVC controller), corresponding view should be created, too (folder under Views).
However, when I do this, nothing happens. Does anybody know what could be the problem?
And will this cause me a problems in a long run? I guess I could create those files manually, but still would prefer if they were generated...
My system:
Visual Studio Professional 2013, Update 5
Project: new MVC Web Application, template "Internet Application"
Thank you
Other possible way is to add complete path of the view in your controller method which is returning a view.
public ActionResult Index()
{
return View("~/Views/Home/Index.cshtml");
}
this method is useful when you have a hierarchy of folders for view files.
Normally views aren't created when a new controller is added. Views can be generated with the following methods:
Simply right click the method name within any controller and select "Add View"
Right click on the controllers folder and select "Scaffold..." which will created a new controller, views and/or DataContext for a specified model.

MVC 5: Controller gets ignored

I'm sure the question is as stupid as simple, but since nobody can give me a hint, I give it a try: I'm learning MVC Asp.Net by creating a small company-internal application, which has only 1 Page.
I worked trough the Tutorial: http://www.asp.net/mvc/tutorials/mvc-5/introduction/getting-started, which helps me a lot.
My problem itself: I went to the Controllers-Folder, added a Controller by selecting "MVC 5 Controller with read/write actions", then in the Views-Subfolder Views\NewItem\ I added a Index.cshtml by selecting "Empty Page".
But now when I debug the Program and select in the URL it keeps shooting a 404-error, meaning the Page is not found. I debugged the Controller, but it doesn't even goes to the Index-Method.
Is there anything I'm missing. I guess it's so stupid and clear I dont even find it on the Internet.
Nevertheless thanks in advance for all answers.
I think the easy way to add a View for your action method that is placed inside the controller you're working at, is to right click inside the body of your action method (that you want the users to access from the browser) and add the View you want, and the MVC automatically will add the View in the right place inside the View folder in Solution Explorer.
Please check you RouteConfig for rout configuration
Ensure that your controller is correctly named - e.g. NewItemController. If it doesn't have the Controller suffix, it won't be picked up.
It should also have an action method matching your view name, e.g. public ActionResult Index() in this case.

How to see all the views in mvc application

I added a default MvcApplication (MVC 4) (its name is MvcApplication3 matching the name of my solution's) width Home views (About, Index, Contact) and that will be my startup (bold in VS solution explorer's interface) project. Then I added another project (MvcApplication, but this time an empty one) called MvcApplication2 to the solution. Then I added the latter project as a reference to the first. I also added a controller called TestController (green line) to the referenced project and generated a view for its Index (red arrow) method. However, when I go to a link /Test or /Test/Index, the view I am expecting (red arrow) is not shown. Then I added the same folder Test with Index.cshtml (blue arrow) to the main project and now I am seeing its contents rather than the project's where my controller sits in.
Is it possible to make the application look for the views in the other project rather than the startup one?
I am adding the image of the structure to make it easier to follow.
P.S.: probably related: the breakpoint IS being hit in the Index method of TestController.
tldr; blue view is used instead of a red one
I think your problema is that you have set MvcApplication3 as startup Project and is causing you to open the view off that Project.
Is it possible to make the application look for the views in the other
project rather than the startup one?
Yes its posible, you can redirect your application the url. Think this your application have a url http://localhost:(someport) you can set redirect to the port of the second application.
I put a link to for better understanding a routing system of MVC: Documentation of routing system
As far as I know, you can't link to projects together like that. Each project becomes its own website with its own address. The reason you might put multiple projects together in one solution is to share things like classes, services, etc. I think what you're needing is areas:
http://www.codeproject.com/Articles/714356/Areas-in-ASP-NET-MVC
With some ideas from me and a friend of mine and a link about overriding RazorViewEngine I finally got what I wanted working exactly how I was expecting it to:
I created a folder named ViewsBase in the main project.
I rewrote RazorViewEngine this way: I only changed the place that was needed for me, leaving everything else like I found in the constructor of RazorViewEngine:
public class MyCustomViewEngine : RazorViewEngine
{
public MyCustomViewEngine()
{
...
ViewLocationFormats = new[]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/{1}/{0}.vbhtml",
"~/Views/Shared/{0}.cshtml",
"~/Views/Shared/{0}.vbhtml",
"~/ViewsBase/{1}/{0}.cshtml",
"~/ViewsBase/{1}/{0}.vbhtml"
};
...
(I find it rather disturbing how I am unable to format the code properly. Can someone give me a hand please?)
and in Global.asax of the main project I added:
ViewEngines.Engines.Clear();
var ourViewEngine = new
ViewEngines.Engines.Add(ourViewEngine);
AreaRegistration.RegisterAllAreas();
...
I added a post-build event command:
xcopy /s "$(ProjectDir)Views\*.*" /Y "$(SolutionDir)$(SolutionName)\ViewsBase\"
It started looking for the views in the expected order
I added a ViewStart file to the base project to make it render the Layout too.

MVC3 My program is looking in the wrong location for the view

I created a project and in it worked hard and created many models, views and controllers. All worked great. I decided to rename one of my models, and accordingly rename my controller and view thinking it would all continue to work great. This is because I wanted to use the origional name for something more appriopriate later. For example.
Old Names:
MyOldModel
MyOldController
MyOldView
Were renamed to:
MyNewModel
MyNewController
MyNewView
Now all still works great execpt that when I click the link to my new view my program looks for and tries to show MyOldView which obviously does not exist. However when I manually put in /MyNewView it works.
How do I change my controller to look for the Index in the MyNewView folder instead of looking for the Index in the MyOldView folder.
I even tried deleting and recreating the controller to no avail.
Thanks in advance for any help.
Edit: To those who have been kind enough to reply so quickly something to note:
The exact steps taken were:
1. Rename the model file from MyOldModel.cs to MyNewModel.cs
2. Rename the controller file from MyOldController.cs to MyNewController.cs
3. Renamed the FOLDER on the view (which only contains Index.cshtml)
from MyOldView to MyNewView
4. At each step visual studio prompted me to rename all refrences to the object being renamed and I accepted (said yes). So the class names all got updated correctly. From what I can see at least, so did all the other refrences.
According to what is being said here, it should be working.
I simply renamed the
As requested by the op, my comment as an answer:
check that your _Layout points to the right views also from button clicks otherwise it will still be searching for the old controllers/views when you have replaced/renamed them with the new controllers/views, glad to be of assistance!
Did you ensure that you renamed the class inside the Controller file, not just the file itself? i.e. so it contains
public class MyNewController : Controller {
....
}
Let's suppose that you have an Index action on the MyNewController:
public class MyNewController: Controller
{
public ActionResult Index()
{
return View();
}
}
Now make sure that you have placed the view in ~/Views/MyNew/Index.cshtml. That's the established convention. Notice that if your controller class is called MyNewController, the folder must be called MyNew. Also don't forget to recompile your web application after renaming the controller class.
Have you done the required modifications to your routes list (registered in Global.asax file i Application_Start() event):
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}

2nd level view in ASP.net MVC

I have an address "http://localhost:3579/MusicStore/StoreManager" which is really showing "http://localhost:3579/MusicStore/StoreManager/Index".
I want to go to another another address on the same level from the index: "http://localhost:3579/MusicStore/StoreManager/Edit". Edit is a view inside the StoreManager folder, so a 2nd level view.
I'm confused as to which controller I'd even put the method in. I tried putting my "public ActionResult Edit" in MusicStoreController, but it wasn't recognized. How can I do this?
It sounds like your action is in the right place, but you will need to make sure there is a route specified to route your URL to that action. Make sure a route like this is specified in your global.asax or area registration file if your project is using areas:
context.MapRoute(
"MusicStore_Edit",
"MusicStore/StoreManager/{action}",
new { action = "Index"}
);

Resources