MVC3 Razor routing from Area to Root - asp.net-mvc-3-areas

I have searched the site and cant find anything similar to my question. I am new to MVC so I may be asking a dumb question.
When the site is launched there are default Home and About buttons at the top. I added a new button that points to an Area, that works just fine.
When I hover over the Home and About buttons after I go to the View that is in my Area, they links on them show they are now pointing to my Area and not the root of my project.
How do I tell these buttons to still point to the root and not my Area?
Thanks for any help or words of wisdom someone will be providing.

This is supposed to take you to root folder.
#Html.RenderAction("Action", "Controller", new { area = "" });
Okay I forgot to ask you what the routes look like in your global.asax.cs file? I've got a similar setup, and here are my route definitions
routes.MapRoute(
"Home", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home",
action = "MaintainLists",
id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // defaults
);

Related

Understanding Default URL in MVC Routing

suppose if I have a route in my RouteConfig.cs file like this
routes.MapRoute(
"pattern1",
"{controller}/App{action}",
new { controller = "home", action = "index" }
);
and when I run my application I get an error HTTP Error 403.14 - Forbidden
but when I add this routes below like this
routes.MapRoute(
"pattern1",
"{controller}/App{action}",
new { controller = "home", action = "index" }
);
routes.MapRoute(
"pattern2",
"{controller}/{action}",
new { controller = "home", action = "index" }
);
then i am able to land to the Default page Home/Index.
My question is why am I not able to land to the default page when I have only one routing configured as shown in the first example.
I have configured the default controller to be Home and action to be Index, then to why am I getting error. Am I missing some concept of MVC routing technique.

I need to change my MVC2 home page to a different page

How can one go about doing that. I am betting it has something to do with the routing engine. Not sure though. I will continue to browse the interwebs...
You're right, you will want to change your default route. It is in the Global.asax.cs file (by default).
The default one looks like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
This line:
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
Is the defaults. The default controller name is Home and the default action is Index. Change them to what you desire.
You can learn more about routing here.
Look at your default route. Out of the box, it will point to the index method of the home controller.

Custom IRouteHandler route screws up MVC link building

I have a custom routehandler in ASP.NET MVC2 to catch all url's at a prefixed path like this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new Route("#api/{*all}", new ApiHandler()));
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Routing works fine, but if i use Html.Actionlink or return ReturnToAction() from a controller, the uri built creates a broken uri like this:
/#api?action=Add&controller=Home
instead of
/Home/Add
How can i influence the uri building logic to consider the Default route pattern?
I've come up with a hack to stop the described behavior. While this "solves" the problem, it's really kind of nasty and I'd prefer a cleaner way to exclude a route from the virtual path building logic
routes.Add(
new Route("#api/{*all}",
// A random and unlikely controller name as the default
new RouteValueDictionary() { { "controller", "qwewqewqeqweq" } },
// and a constraint requiring any controller to be that random, default value
new RouteValueDictionary() { { "controller", "qwewqewqeqweq" } },
new ApiHandler()
);
This means that the route would only be chosen for Virtual Path building if the controller in question was "qwewqewqeqweq", which hopefully is unlikely. I said it was nasty.
You could specify the name of the route using the RouteLink helper:
<%: Html.RouteLink("link text", "Default",
new { action = "add", controller = "home" }) %>

How to change the starting page of the project?

I create a new project and start it. It turns out that the controller Home is called with the action is default i.e. Index - this means Controllers/HomeController.Index() is called. The view displayed by this action is defined in this method - i.e. Views/Home/Index.apsx.
I want to change this starting behavior to be another controller and action. How can I do this?
Open the file Global.asax.cs and look for the line
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
There, change Home and Index to the your choice's values.
Hope that helps!

Custom route doesn't find controller action

For some reason my application isn't routing to my controller method correctly. I have a routelink like this in my webpage -
<%= Html.RouteLink("View", "Blog", new { id=(item.BlogId), slug=(item.Slug) }) %>
In global.asax.cs I have the following routes -
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"MoreBlogs",
"Blog/Page/{page}",
new { controller = "Blog", action = "Index" }
);
routes.MapRoute(
"Blog",
"Blog/View/{id}/{slug}",
new { controller = "Blog", action = "View"}
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Blog", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
And then I have a class BlogController that has a method -
public ActionResult View(int id, string slug)
{
... etc.
}
I put a breakpoint in the first line of the View method but it's not getting hit at all. I checked with a route debugger for the format localhost/Blog/View/1/test and it matched my custom route. All I'm getting is a 404 while running this, I can't work out why the route won't post to the view method in my controller - any ideas?
(Assuming the route debugger was based on Phil Haack's debugger post)
If you're getting a 404, that makes me think the actual view page itself cannot be found.
Assuming that you are also using areas within the application, and that RouteLink is actually being called from within an Area, I think you may need to specify area="" in your routeValues object ( you also need to specify the controller). In general, I think that you will need to add the area="..." part with all your Routelink calls when using Areas.
This is something that I have picked up about RouteLink and Area, but cant seem to find any reference material detailing the limitations.