Change default view in VS 2010 - asp.net-mvc-3

How to set change a default View in MVC3 when debbuging the project in Visual Studio 2010.
As soon I hit F5, The default View it opens is Localhost/Home/Index.
Where is it being set, How do I update it?
Can anyone shed some light on this please? It is not straight forward(for me though).
Thank you

All you have to do is changed your default MapRoute parameters. Typically, this is what you'll see by default as your Default route:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home",
action = "Index",
id = UrlParameter.Optional }); // Parameter defaults
Just change the controller property and the action property to what you want your default to be. For instance, you could do:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "AnotherController",
action = "aDifferentAction",
id = UrlParameter.Optional }); // Parameter defaults
All that is changed here is the controller and action properties. Now when you browse to just the qualified name, it will go to your AnotherController.aDifferentAction() method, instead of the HomeController.Index() method.
Explanation
The reason why it defaults to Home.Index(), is because that is the first matched route when you have empty route parameters for controller and action. By changing these defaults in the MapRoute() call, you are telling routing that if nothing is there for the route parameters, go to AnotherController.aDifferentAction() action method.
As long as this is the first route, you should be set.

You can set the default page in the Routes table as Shark suggests, but something tells me this probably isn't really what you're looking for. If you just want to debug a specific page, right-click the view and select 'View In Browser' from the context menu.

Related

Why #Html.ActionLink("Home", "Index", "home") generates Home

When I use Html.ActionLink, I found that #Html.ActionLink("Home", "Index", "home") generates this string Home, so why the address \home\index becomes \, I found this is related with default route which setted in web.conf. I have viewed the mvc 3 source code, but I can't find answer. Who can help me? Thanks.
That's due to the default route. If you don't specify default action and controller it will generate /home/index as url.
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { id = UrlParameter.Optional }
);
routing is bidirectional:
- in: it converts incoming urls into controller/actions/parameters
- out: it converts controller/actions/parameters into urls

ActionLink behavior with ASP.NET WCF and Routes.Add in MVC application

I'm want to host both WCF 4 and MVC 3 in my C#.Net project. But when I add the service paths for WCF, Html.ActionLink starts creating a wrong url for MVC app. My route table is created as:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
RouteTable.Routes.Add(new ServiceRoute("api1/projects", new WebServiceHostFactory(), typeof(Projects)));
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Above route table creates the right access paths to both the WCF and MVC applications, but Html.ActionLink creates the edit links as
http://localhost:8000/api1/projects?action=Edit&controller=technology&id=2
instead of
http://localhost:8000/technology/Edit/2
If I omit the line starting with RouteTable.Routes.Add, the ActionLink works as expected (and of course not the WCF). How can I add the WCF routes and make sure actionlink behaviour doesn't change?
Try putting ServiceRoute registration after MapRoute.

How do I route to /Configuration/ URL in ASP.NET MVC3?

This is for an ASP.NET MVC3 web application. In RegisterRoutes I have the following as my only line:
routes.MapRoute("Default", "Configuration", new { controller = "DeviceConfiguration", action = "Index" });
When I run the project, going to the URL /Configuration/ gives me a 404 error. However if I change the word Configuration to any other word, such as:
routes.MapRoute("Default", "Configuratio", new { controller = "DeviceConfiguration", action = "Index" });
Then going to the URL /Configuratio/ loads just fine. It seems as if ASP.NET is simply refusing to route to the URL /Configuration/.
Again, this is the only line in RegisterRoutes; I've tried commenting out everything else to debug this. I have no MapRoute or IgnoreRoute calls in my code anywhere else, and I am not editing the routing table in any location.
How can I change this behavior?
I suspect that you have a physical folder called Configuration under the root of your application. The ASP.NET MVC routing engine has preference for physical folders over routes. One possible way is to set the RouteExistingFiles property to true after your route definition:
routes.MapRoute(
"Default",
"Configuration",
new { controller = "DeviceConfiguration", action = "Index"
});
routes.RouteExistingFiles = true;

Url.Action to show page no in url

Trying like this
Url.Action("Index", "Home", new { page = 5 })
is giving me url like
/Home/Index?page=5
How to get a url like this
/Home/Index/5
By defining a route:
routes.MapRoute(
"PagedRoute",
"{controller}/{action}/{page}",
new { controller = "Home", action = "Index", page = UrlParameter.Optional }
);
And be careful with the default route (the one that uses an id) as it is similar. You will might need to put this custom route before the default one or remove the default route as it rarely be hit under those circumstances.
I would recommend you going through the Routing tutorials to gather deeper understanding of how they work.

mvc application with razor data view engine problems with the url

I create a new mhc application with razor data view engine. I have a problems with the url
Here is my action links
#Html.ActionLink("Home", "Index")
#Html.ActionLink("Schedule", "Schedule")
After I loaded home page my url looks fine
Example: mysiteurl.com
Then I click Schedule link (if I hover I see the correct url http://mysiteurl.com/home/schedule). If I click it as a result my url will http://mysiteurl.com//#/Home/Schedule. I don't know why its adding # sign to my url but it's causing the issue in my application with other pages.
any idea what I'm doing wrong?
I don't have any custom routing
here is my RegisterRoutes method in Global.asax
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
);
}
Your ActionLinks look fine. I'd guess you have at least one custom route defined inside your RegisterRoutes method. I'd guess you've got a typo somewhere in there.
You could try installing Glimpse via NuGet - it allows you to see exactly which routing rules are firing. Scott Hanselman has this blog on how to use it.

Resources