Help with MVC view - asp.net-mvc

When I create new MVC 3 project in Visual Studio the first thing I do is create a new controller called Home, and then right click in the Index stub and create a new view.
My question is when I have the Index file selected in the solution explorer and build the solution I get an error saying it can't be found. If I navigate to the page using root/ Home or root/Home/Index it still doesn't work.
I also created a new project using the sample website that ships with MVC and cannot figure out what code differs between an empty solution and the sample solution that could be giving me this problem. In the global.asax it looks like there is already a route setup for a home controller so I'm confused.

Your controller class should be called HomeController, not Home. On the screenshot you've shown I see that you've called it Home. By convention all controller classes in ASP.NET MVC must have the Controller suffix. In Global.asax you should have a default routing rule which states:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
which means that when you run the site and you request / it will look for the Index action on the HomeController.

I am unable to reproduce with Microsoft Web Developer 2010 Express, ASP.NET MVC 3 and creating a new empty Razor-view project. When you choose "Add View" did you enable "Create as a partial view"? If so, that may be the issue. Did you enable "Use a layout or master page" and select an existing master page? If not, that may be the issue...
The routing looks fine in an empty project. I see the Home/Index view with these URLs:
/
/home
/home/index
Post problematic project as .zip?

Related

A default document is not configured for the requested URL

Creating a new section to my MVC site called "Reports".
Created a simple controller and starter Index view.
If I try to browse to ".../Reports/Index" things work fine. I am greeted with my page.
If I try to browse to ".../Reports", I get the message listed in the title. (...default document not configured...)
When I try the same with another section like ".../Customers", I get my index page... so, I think my routing is setup properly.
Here is my RouteConfig just in case:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional }
);
I must be missing something simple related to the new reports page, but I just can't see it. I need some fresh eyes.
I had another folder name "Reports" at the top level of the directory tree in my project. I had planned to, well, put the reports in there. Anyway, MVC did not know which folder to route to, since there were to folders name "Reports" in my project.
Just to test the fix I renamed the report storage location to "Report" (dropped the "s"), ran again and tada... MVC is happy again.
I suspect the issue was because you had a SQL server with SQL Server Reporting enabled, as it will take over the Reports folder name.
As The answerer pointed out if you rename the folder it avoids SQL server Reportings use of the alias

MapRoute in mixed web forms/MVC app

I have an older web-form app and now I am trying to add new pages using MVC.
It all seems to work just fine except one thing:
The application's default page (login.aspx) is a web form.
When user hit link www.mysite.com, instead of opening www.mysite.com/login.aspx, the site immediately goes to route specified in global.aspx as
routes.MapRoute("FrontLine", "{controller}/{action}/{id}",
new { controller = "FrontLine", action = "QuickView", id = "" });
So, the question is how to make login.aspx a default page?
Is it possible to do without converting login.aspx to a MVC view and adding corresponding controller?
Found a solution:
excute line:
routes.MapPageRoute("Default", "","~/Login.aspx")
before calling
routes.MapRoute

Default route problem

If I want to hit a url like
http://localhost:8080/controllername
I want the "Index" action to be the default action called. I assumed the default route mapping would be fine and the "Index" action would be called on whatever controller was specified - seems I need to specify
http://localhost:8080/controllername/index
Is this correct?
Mapping:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
What you're trying should definitely work. In fact, the code you posted is from the default templates, and I've just tested it by adding an "Index" action to the AccountController and visiting /Account in my browser.
I'd recommend creating a new project and testing this behaviour (first with the built-in web server, then with IIS, if you're not always using the built-in server). If it works, there's probably something different in your project that's causing the issue.
I had a similar problem and it occured because of a collision with a directory in the project. I had a structure like this in my project:
Controllers \
HomeController.cs
CmsController.cs
Cms \
WhateverFile.cs
The Cms subdirectory collided with the /Cms URL, while Cms/Index worked. I simply renamed my colliding folder name. If you have to keep it, there is a RouteCollection.RouteExistingFiles that can be used to prevent automatic lookup of files. If that is enabled I think that a lot exclusions have to be added for the Script etc, see this blog post for an example.

How do you set the startup page for debugging in an ASP.NET MVC application?

How do you start debugging the application at the application root? For example: http://localhost:49742/
I'm always getting a page which doesn't exist, such as:
http://localhost:49742/Views/Home/About.aspx
Note that it would be OK to start at http://localhost:49742/Views/Home/About
Go to your project's properties and set the start page property.
Go to the project's Properties
Go to the Web tab
Select the Specific Page radio button
Type in the desired url in the Specific Page text box
While you can have a default page in the MVC project, the more conventional implementation for a default view would be to use a default controller, implememented in the global.asax, through the 'RegisterRoutes(...)' method. For instance if you wanted your Public\Home controller to be your default route/view, the code would be:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Public", action = "Home", id = UrlParameter.Optional } // Parameter defaults
);
}
For this to be functional, you are required to have have a set Start Page in the project.
This works for me under Specific Page for MVC:
/Home/Index
Update: Currently, I just use a forward slash in the "Specific Page" textbox, and it takes me to the home page as defined in the routing:
/
Selecting a specific page from Project properties does not solve my problem.
In MVC 4 open App_Start/RouteConfig.cs
For example, if you want to change startup page to Login:
routes.MapRoute(
"Default", // Route name
"", // URL with parameters
new { controller = "Account", action = "Login"} // Parameter defaults
);
If you want to start at the "application root" as you describe right click on the top level Default.aspx page and choose set as start page. Hit F5 and you're done.
If you want to start at a different controller action see Mark's answer.
Revisiting this page and I have more information to share with others.
Debugging environment (using Visual Studio)
1a) Stephen Walter's link to set the startup page on MVC using the project properties is only applicable when you are debugging your MVC application.
1b) Right mouse click on the .aspx page in Solution Explorer and select the "Set As Start Page" behaves the same.
Note: in both the above cases, the startup page setting is only recognised by your Visual Studio Development Server. It is not recognised by your deployed server.
Deployed environment
2a) To set the startup page, assuming that you have not change any of the default routings, change the content of /Views/Home/Index.aspx to do a "Server.Transfer" or a "Response.Redirect" to your desired page.
2b) Change your default routing in your global.asax.cs to your desired page.
Are there any other options that the readers are aware of? Which of the above (including your own option) would be your preferred solution (and please share with us why)?

How do I get rid of Home in ASP.Net MVC?

I know this site is written using ASP.Net MVC and I do not see "/Home" in the url. This proves to me that it can be done. What special route and do I need?
Just change "Home" to an empty string.
routes.MapRoute(
"Home",
"",
new { action = Index, controller = Home }
);
If you're running on IIS 7, you can simply delete the Default.aspx file that comes with ASP.NET MVC (assuming you're running on Preview 3 or higher). That file was needed due to an issue with Cassini that was fixed in .NET 3.5 SP1. For more details check out:
http://haacked.com/archive/2008/04/10/upcoming-changes-in-routing.aspx
and
http://haacked.com/archive/2008/05/12/sp1-beta-and-its-effect-on-mvc.aspx
I actually like having all of my home controller methods to be at the root of the site. Like this: /about, /contact, etc. I guess I'm picky. I use a simple route constraint to do it. Here is my blog post with a code sample.
I'd add
routes.MapRoute("NoIndex", "{action}", new { controller = "Home", action = "Index" });
in RouteConfig.cs
This is what I did to get rid of Home. It will treat all routes with only one specifier as Home/Action and any with two as Controller/Action. The downside is now controller has to have an explicit index (/Controller != /Controller/Index), but it might help you or others.
routes.MapRoute(
"Default",
"{action}",
new { controller = "Home", action = "Index" }
);
routes.MapRoute(
"Actions",
"{controller}/{action}",
new { }
);
In IIS 7, you can simply delete the Default.aspx file that comes with ASP.NET MVC (assuming you're running on Preview 3 or higher). That file was needed due to an issue with Cassini that was fixed in .NET 3.5 SP1.
For more details check out:
Upcoming Changes In Routing and .NET 3.5 SP1 Beta and Its Effect on MVC

Resources