Default route in MVC project with areas - asp.net-mvc

I have an ASP MVC 4 project which contains areas and is structured as follows:
Areas
MyArea
Controllers
MyAreaController.cs
MyAreaRegistration.cs
Controllers
HomeController.cs
Global.asax
In Global.asax, I register first the area routes and then the global routes:
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
I have a default route in RegisterRoutes:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "Controllers" }
);
Now, I want the default route of the project to point to an action (the login page with an return url as parameter) in MyArea without affecting the rest of the routes. I tried to set a default route in MyAreaRegistration:
context.MapRoute(
"MyArea_Default",
"{controller}/{action}/{returnUrl}",
new { controller = "MyArea", action = "LogOn", returnUrl = "/Home/Index" },
new[] { "Areas.MyArea.Controllers" }
);
This is working, the projects starts on the desired view. However, the problem is that many routes used across the project (for example /Home/Help) will match the MyArea_Default route since it is registered before the Default route. Trying to set the area DataToken in RegisterRoutes also messes up every other route not in the area.
How can I achieve a default route in the area without messing up everything else?
Any help is appreciated.

Related

I want to call controller's Default action.

Example : My User will enter www.xyz.com/Promo/PROMO123
where "PROMO123" is value, which i require.
above code produces error :
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
However
www.xyz.com/Promo/Index/PROMO123 will work properly,
but i dont want this.
How can i archive this
www.xyz.com/Promo/PROMO123
Have you tried Routing?
Such as
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//Don't forget to add this before default one.
routes.MapRoute(
name: "PromoRoute",
url: "{controller}/{myString}",
defaults: new { controller = "Promo", action = "Index", myString = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
You need to define a route pattern for this.
If you want this to work application wide, then you will need to change the default route. But I would suggest simply adding a specific route for this controller, in addition to the default route, because you probably don't want to override the default MVC routing for the whole app or you will lose the ability to use multiple actions per controller.
See your RouteConfig, try this route (MVC pre-5):
routes.MapRoute("myRoute", "PromoRoute/{id}",
new {controller="PromoRoute", action = "Index"});
With MVC5 you can add this directly to your action assuming you've enabled attribute routes:
[Route("PromoRoute/{id}")]
public ActionResult Index(string id) {
}

ASP.NET MVC 3 Routes Always Have Querystring Value "Area="

This is an annoyance that I've experienced for a long time, but now my client is asking me to address it.
In every route that gets generated (by a non-Default route), a query string value gets appended: "Area="
As an example:
// RouteConfig.Register():
routes.MapRoute(
"ProfileDetails",
"{slug}",
new { controller = "Profile", action = "Details" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
To generate a URL to the BadgeController.Index action, the Default route will be applied and the result will be /Badge... and that's what is expected.
But to generate a URL to the ProfileController.Details(someUser) action, the ProfileDetails route will be applied and the result will be /someUser?Area= ... which will work, but the ?Area= is unnecessary and messy.
I have no areas in my project. How do I get rid of that Area= query string value? This happens with all of my routes that are not the predefined Default route, not just the "ProfileDetails" one in this example.
I've tried removing the AreaRegistration.RegisterAllAreas() from my Global.asax file, since I assume it's not required.

Routing to an Area home page in ASP.NET MVC

I'm trying to route to the home page of an Area in MVC, e.g.
myDomain.com/myArea/Home/Index
when I use the URL:
myDomain.com/myArea
MVC appears to by trying to find a Controller called "myArea" in the root Controllers folder and thereby would route to:
myDomain.com/myArea/Index
if the Controller existed.
Again, I want:
myDomain.com/myArea
to route to:
myDomain.com/myArea/Home/Index
I figure that I should be able to do this in Global.asax without having to resort to creating a dummy controller that re-routes to the area controller, but I've drawn a blank.
One way I used to get around this was to set the namespaces property on the default route in the Global RegisterRoutes method, this seemed to resolve that problem. Had to add this restriction to some other routes where I had conflicts between Controller in the main website and the area.
var namespaces = new[] { "CompiledExperience.Website.Web.Controllers" };
routes.MapRoute("Default", "{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces
);

/Mappings/Index is found but not /Mappings with ASP.NET MVC

Just struggling with a simple issue with ASP.NET MVC. I have a list of views, each view associated with an Index.aspx view being associated by default with /MyView.
Yet, for some reason I have 1 view named /Mappings that does not work (404 resource is not found) whereas the explicit path /Mappings/Index works.
I have the default route settings as provided by the default ASP.NET MVC sample
routes.MapRoute(
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
And, the default Index works for the other views of the same webapp.
Any idea what could be wrong here?
You have to define default action if it is not provided:
route.MapRoute(
"Default", // Route name
"{controller}/{action}", // URL with parameters
new { action = "Index" } // Default action if not provided
);
EDIT:
Look at this link:
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
You can use this debugger to test your routing.

Asp.net MVC routing ambiguous, two paths for same page

I'm trying out ASP.NET MVC routing and have of course stumbled across a problem. I have a section, /Admin/Pages/, and this is also accessible through /Pages/, which it shouldn't. What could I be missing?
The routing code in global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Pages", // Route name
"Admin/Pages/{action}/{id}", // URL with parameters
// Parameter defaults
new { controller = "Pages", action = "Index", id = "" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
// Parameter defaults
new { controller = "Home", action = "Index", id = "" }
);
}
Thanks!
I'd suggest adding an explicit route for /Pages/ at the beginning.
The problem is that it's being handled by the Default route and deriving:
controller = "Pages"
action = "Index"
id = ""
which are exactly the same as the parameters for your Admin route.
For routing issues like this, you should try out my Route Debugger assembly (use only in testing). It can help figure out these types of issues.
P.S. If you're trying to secure the Pages controller, make sure to use the [Authorize] attribute. Don't just rely on URL authorization.
You could add a constraint to the default rule so that the {Controller} tag cannot be "Pages".
You have in you first route {action} token/parameter which gets in conflict with setting of default action. Try changing parameter name in your route, or remove default action name.

Resources