I have searched on Google (may be with wrong keyword) and visited the tutorials of ASP.NET/MVC website. But didn't understand the routing of MVC properly. I have a scenario of the following figure. I want to开发者_开发百科 display the Index.cshtml when the website lunched.
I have changed the RegisterRoutes method of Global.asax.cs file in various ways. The latest code (not working) is below
routes.MapRoute(
"App", // Route name
"App/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I will be very happy if someone explains the MapRoute method with different example along with answering my problem.
I don't think the problem is with your routes, I believe it is caused by moving your View and Controller folders to the App folder.
MVC use "convention over configuration" for stuff like where the folders are located, I'm guessing it can't find the Views/Controllers in the new folders?
From here:
These folders are included even in an Empty ASP.NET MVC application because the ASP.NET MVC framework by default uses a “convention over configuration” approach and makes some default assumptions based on folder naming conventions. For instance, controllers look for views in the Views folder by default without you having to explicitly specify this in your code. Sticking with the default conventions reduces the amount of code you need to write, and can also make it easier for other developers to understand your project.
I'm not saying that it's not possible to have a folder structure like you have, I just dont believe its supported out of the box. Personally I'd recommend moving back to the default, if you need to keep this structure then I think you may need to implement your own ControllerFactory and ViewEngine (I'm not sure if these are the correct hooks that you would need to change).
Edit: Are you trying to implement App as an Area?
Edit2: If you are trying to add App as an area there are some steps you will need to follow, The reason I wasn't sure if this was what you were trying to do is because I thought areas had to exist within an Areas folder. There are some steps outlines here Including how to register your area if that is what you are trying to do.
ASP.NET MVC 3 has a built in feature for sub folders. That is called Areas (thanks @Morten for let me know the word).
But after finishing the MSDN Walkthrough for Areas, I was still unable to run the web application. The error was showing Configuration errors. @Danny also noticed that.
Then I searched and found that, the Web.config files in the sub folders shouldn't contain application specific properties.
So, I removed those properties from the Web.config files. Then I was able to run. But there were some runtime errors when I wanted to navigate to my sub folder/area views. The error was "Could not load type ‘System.Web.Mvc.ViewPage<..<>’" when I click on the links for the views of the Areas folder.
Then I again searched and found helpful this post. And then my application successfully run and I can navigate all of the pages.
精彩评论