routing action within mvc4 area not able to map action - asp.net-mvc

I am having trouble trying to get a route working in an area I have.
My area is called ABC and I have a controller called Home within the area. I am able to hit the breakpoint with Home/Index if I browse "http://localhost:8000/abc" however when I try hit another action called details like "http://localhost:8000/ABC/details" I get a 404.
I have tried
context.MapRoute(
"details",
"ABC/Home/{action}/{id}",
new { action = "details", id = UrlParameter.Optional },
constraints: null,
namespaces: new[] { "WebApplication.Areas.ABC.Controllers" }
);
context.MapRoute(
"ABC_Home",
"ABC/{controller}/{action}/{id}",
new { controller = "home",action="Index", id = UrlParameter.Optional },
constraints: null,
namespaces: new[] { "WebApplication.Areas.ABC.Controllers" }
);
This allows me to hit the action if I use "http://localhost:8000/ABC/Home/Details"
context.MapRoute(
"details",
"Home/Home/{action}/{id}",
new {controller="home", action = "details", id = UrlParameter.Optional },
constraints: null,
namespaces: new[] { "WebApplication.Areas.ABC.Controllers" }
);
Ideally I don't want to use home within the url if possible. What am I doing wrong?
Any help would be awesome!

I think you would just need a single route for this. Don't include the controller in the route, since it seems to be implied by starting with /ABC; just assign the controller as a default value:
context.MapRoute(
"ABC_Home",
"ABC/{action}/{id}",
new { controller = "home", action="Index", id = UrlParameter.Optional },
constraints: null,
namespaces: new[] { "WebApplication.Areas.ABC.Controllers" }
}
Per your requirements, this will route /abc to /home/index, and will route /abc/details to /home/details.
Then, if you need to access other controllers you can add another rule for that, something like the default one:
context.MapRoute(
"Default_Route",
"{controller}/{action}/{id}",
new { id = UrlParameter.Optional }
}

I don't think you can default a controller with a variable action name, otherwise there's no way to tell from the routes that it is an action or controller and which route to match. I think you could hard-code the action though:
Context.MapRoute(
"ABC_Home_Details",
"ABC/Details/{id}",
new { controller = "home", action="details", id = UrlParameter.Optionsl },
constraints: null,
namespaces: new [] { "WebApplication.Areas.ABC.Controllers" }
);

Related

NET MVC RegisterRoutes rule

routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "Teng.Web.Controllers" });
routes.MapRoute(
"CMSArticle",
"{Classify}/{controller}/{action}/{id}",
new { Classify = #"", controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "Teng.Web.Controllers" });
To match CMSArticle http://localhost:4848/ss/home/index/5
I want to http://localhost:4848/ss/home/index
go CMSArticle Routes
home and ss both seems controller names. you have to go for default routes. but before that check your Url.
http://localhost:4848/ss/home/index/5 - Check the ss. normall it comes as
http://localhost:4848/home/index/5
Is classify an actual parameter? I believe they need to be in order of importance. If the route doesn't match to one it falls to the next. Try the below.
routes.MapRoute(
"CMSArticle",
"ss/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "Teng.Web.Controllers" });
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "Teng.Web.Controllers" });
You can also set specific controller/action in the route so that it doens't work for all controllers and/or actions.

MVC5 Custom Route to Remove Controller Name from Area

So far I can't make this work and I need some assistance please. I have a basic MVC 5 site, and I have added an area called Administration. For the life of me, I can't figure out how to set a default controller/action for an area, properly.
In my site, I have an area named Admin, a controller named Admin (with Index method and view), and this is the area registration:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "Admin_base",
url: "Admin",
defaults: new { area = "Admin", controller = "Admin", action = "Index", id = UrlParameter.Optional }
);
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
The first MapRoute allows me to browse to http://myapplication/Admin and it displays a view that I set (Admin/Index) and the URL stays http://myapplication/Admin (which is what I want). Now, by adding this, it breaks any further routing to a controller. So when I attempt to navigate to the Menu controller in the Admin area, it fails.
Is there a correct way to do this?
i.e. I need to make http://myapplication/Admin/Menu/Create route properly, but also need to retain the default Controller/Action for the area.
You should just be able to combine them into one:
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
defaults: new { area = "Admin",
controller = "Admin",
action = "Index",
id = UrlParameter.Optional }
);
By assigning defaults, you should be able to call /Admin/ and the rest of the parameters are set to the defaults.
If you are using the same controller name for Area and default one(For eg: HomeController). Use the namespaces for that
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new {
area = "Admin",
controller = "Home",
action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "{{Namespace}}.Areas.Admin.Controllers" }
);
for default one
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index",
id = UrlParameter.Optional },
namespaces: new[] { "{{Namespace}}.Controllers" }
);

Inserting a new route into the RouteTable

I want to insert a route into the route table but can't work out how to do it.
For example I have a route mapped as below:
routes.MapRoute(
name: "Default",
url: "",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
However I want to insert that same route into the route table using the syntax below. How do I do it?
RouteTable.Routes.Insert(0, new Route(
Thanks for your help.
After a bit of thinking I found the way to do it:
Route myRoute = new Route("", new RouteValueDictionary { { "controller", "Home" }, { "action", "Index" }, {"id", UrlParameter.Optional }}, new MvcRouteHandler());
RouteTable.Routes.Insert(0, myRoute);
The routes you have there is the RouteTable.Routes and by MapRoute this is being added to the tables. You can only add each route once.

How to configure routes in asp.net mvc with Areas

I am working on asp.net mvc 3. I have three areas in my project like,
MyProject/Areas/Blogs
MyProject/Areas/Forums
MyProject/Areas/Groups
Among these three, blogs view is startup view. for that i have set the globla.ascx as
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Blog", action = "Blog", id = UrlParameter.Optional }
);
and in BlogAreaRegistration.cs,
context.MapRoute(
"Blogarea_Default",
"{controller}/{action}/{id}",
new { controller = "Blog", action = "Blog", id = UrlParameter.Optional }
);
and in ForumAreaRegistration.cs,
context.MapRoute(
null,
"Forums/{action}/{id}",
new {controller="Forums", action = "Forum", id = UrlParameter.Optional }
);
and in GroupsAreaRegisration.cs,
context.MapRoute(
"Groups_default",
"Groups/{controller}/{action}/{id}",
new { controller = "Groups", action = "Group", id = UrlParameter.Optional }
);
Here Forum and Blog are work as i desire but the Group does not work it always shows 404 Resource not found page so please guide me if i did any mistake in the process.
Try change
context.MapRoute(
"Groups_default",
"Groups/{controller}/{action}/{id}",
new { controller = "Groups", action = "Group", id = UrlParameter.Optional }
);
for:
context.MapRoute(
"Groups_default",
"Groups/{action}/{id}",
new { controller = "Groups", action = "Group", id = UrlParameter.Optional }
);
You should remove either Groups/ or {controller}/ from your GroupsAreaRegisration.cs code block - then it should work. I'd remove {controller}/, so the code will be:
context.MapRoute(
"Groups_default",
"Groups/{action}/{id}",
new { controller = "Groups", action = "Group", id = UrlParameter.Optional }
);
Also in BlogAreaRegistration.cs I'd replace {controller} with Blog, as you might get quite unexpected results otherwise. The complete code here will be
context.MapRoute(
"Blogarea_Default",
"Blog/{action}/{id}",
new { controller = "Blog", action = "Blog", id = UrlParameter.Optional }
);

Mvc Action Link not using correct route

I have the following routes and typing them in the browser works fine and routes correctly, but if I use a Html.ActionLink, it tries using the DefaultStuff Route.
Routes
_routes.MapStuffRoute(
"DefaultStuff",
"stuff/{controller}/{id}",
new { id = UrlParameter.Optional },
new[] { typeof(BaseApiController).Namespace });
_routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { typeof(BaseController).Namespace });
Page
#Html.ActionLink("Job Queues", "Index", "Job") // generates http://localhost/stuff/job?action=Index
What am I missing to allow ActionLink to generate http://localhost/stuff/index. Reversing the routes the ActionLink is correct but the Stuff does not work. Just a note, the StuffRoute sets the action name based on the information in the request.
It seems as though you actually are trying to map the controller "Job" to stuff. Currently your "DefaultStuff" route does not resolve an action, so it is putting it in as a query string value.
_routes.MapRoute(
"DefaultStuff",
"stuff/{action}/{id}",
new { controller="Job", id = UrlParameter.Optional },
new[] { typeof(BaseApiController).Namespace });

Resources