How to configure routes in asp.net mvc with Areas - asp.net-mvc

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 }
);

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.

ASP.NET MVC 5 - Removing controller name from url fails for other controllers

I followed the answer to this question but when I apply it I get an error if I try to access views under other controllers.
If I go to http://mydomain/MyActionUnderHome it works fine, but if I go to http://mydomain/SomeOtherController/MyAction it throws
"The resource cannot be found."
Shouldn't the Default route take over if the URL doesn't match the route definition above the Default route?
Are there perhaps new ways in MVC 5 to do this?
My routes:
routes.MapRoute(
"HomeRoute",
"{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"AccountRoute",
"{action}/{id}",
new { controller = "Account", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Better to use Route attribute.
public class HomeController : Controller
{
[Route("")]
public ActionResult Index() { return View(); }
[Route("MyAccount")]
public ActionResult Account() { return View(); }
}
Then add this line t your RouteConfig class before the routes.MapRoute
routes.MapMvcAttributeRoutes();
The issue is that you have is conflicting routes that the router can't resolve correctly
routes.MapRoute(
"HomeRoute",
"{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"AccountRoute",
"{action}/{id}",
new { controller = "Account", action = "Index", id = UrlParameter.Optional }
);
When the router looks at the path /youraction/yourparameter it will resolve to the first route.
The next part of this is that when you provide two values as your URL (as in your example the default router never gets called because the router interprets your input as /action/id rather than controller/action/id
If you change your routes to be:
routes.MapRoute(
"HomeRoute",
"index/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Then you'll get your effect. But your second route can still never be called, as your scheme isn't correct.

MVC Routes for an multi tenant application

I have a MVC application and can't quite get the routing working for my multi-tenant application. Here is the problem:
I have 2 types of pages in my application, most require the tenant name to be in the url but some don't. e.g.
These Do (tenant name is these examples is samsung and apple):
http://www.mytestapp.com/samsung/customers/add
http://www.mytestapp.com/apple/customers/add
These Don't:
http://www.mytestapp.com/home/register/
http://www.mytestapp.com/home/aboutus/
What routes do i require to get this working? I have tried this but it does not work for the register and about us page.
routes.MapRoute(
name: "TenantRoute",
url: "{tenantid}/{controller}/{action}/{id}",
defaults: new { tenantid = "tenantname", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Well, you'll need a second route to match the non-tenant routes. The default one should match if register and aboutus are controllers with an Index action:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Register", action = "Index", id = UrlParameter.Optional }
);
try with this
routes.MapRoute(
"samsung",
"samsung/{controller}/{action}/{id}",
new { controller = "YourController", action = "YourAction", id = UrlParameter.Optional}
);
routes.MapRoute(
"apple",
"apple/{controller}/{action}/{id}",
new { controller = "YourController", action = "YourAction", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Register", action = "Index", id = UrlParameter.Optional }
);

ASP.NET MVC3 not right index page because of routes

I have these routes:
routes.MapRoute(
"ActionOnly",
"{action}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { action = "Klub|Historie" });
routes.MapRoute(
"Administrace", // Route name
"Administrace/{controller}/{action}/{id}", // URL with parameters
new { controller = "Administrace", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
// Hrac/Jmeno_hrace
routes.MapRoute(
"Hrac",
"Hrac/{name}",
new { Controller = "Hrac", Action = "Name" }
);
// pro aktivaci uzivatele který se registroval, ale jeste nepotvrdil email
routes.MapRoute(
"Activate",
"Account/Activate/{username}/{key}",
new { controller = "Account", action = "Activate", username = UrlParameter.Optional, key = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Why #Html.ActionLink("Domů", "Index", "Home") is creating website.com/Administrace/Home and not website.com/Home/index and how can I fix it?
Your Administrace route is swallowing all controllers.
You should change it to hard-code the controller name:
routes.MapRoute(
"Administrace", // Route name
"Administrace/Administrace/{action}/{id}", // URL with parameters
new { controller = "Administrace", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
If you want that route to work for multiple controllers, you should replace it with an area (or just add a constraint).
Use #Html.RouteLink instead with the route name being default
Link to something that might help:
What's the difference between RouteLink and ActionLink in ASP.NET MVC?

MVC 3 Route problem

ActionLink result "http://localhost:5089/Article/GetArticlesByCategory?category=ASP.NET&categoryId=2". i want to show that link type "http://localhost:5089/Blog/ASP.NET". what is wrong route named "Article".
Routes:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Article",
"Blog/{category}", //
new { controller = "Article", action = "GetArticlesByCategory", category = UrlParameter.Optional, categoryId = UrlParameter.Optional }
Link:
#Html.ActionLink(k.Name, "GetArticlesByCategory", "Article",
new { category = k.Name, categoryId = k.CategoryId }, null)
SOLVED
GetArticlesByCategory parameter int categoryId changed to >> string category and replaced action codes as to new parameter (string category)
Routes replaced with:
routes.MapRoute(
"Category",
"Blog/{category}",
new { controller = "Article", action = "GetArticlesByCategory", category = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "index", id = UrlParameter.Optional } // Parameter defaults
);
ActionLink replaced with:
#Html.ActionLink(k.Name, "GetArticlesByCategory", "Article",
new { category = k.Name }, null)
There are a few issues. First, and most important, your routes are specified in the wrong order. The default route should be defined last. Second, never define a route with two optional parameters. It just causes too many problems.
Try the following for your routes:
routes.MapRoute(
"CategoryAndId",
"Blog/{category}/{categoryId}",
new { controller = "Article", action = "GetArticlesByCategory" }
);
routes.MapRoute(
"CategoryOnly",
"Blog/{category}",
new { controller = "Article", action = "GetArticlesByCategory",
category = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "index",
id = UrlParameter.Optional } // Parameter defaults
);
You are not specifying the action in the route
routes.MapRoute(
"Article",
"Blog/{action}/{category}/{categoryId}", //
new { controller = "Article", action = "GetArticlesByCategory", category = UrlParameter.Optional, categoryId = UrlParameter.Optional }
I suggest you use Phil Haack's routes debug, http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx. A great way for debugging your MVC routes
If you want the link to show as http://localhost:5089/Blog/ASP.NET, you'll need to change the actionlink as such:
#Html.ActionLink(k.Name, "GetArticlesByCategory", "Article",
new { category = k.Name }, new { #title = "Kategorisindeki Makaleler", #class = "selected" })
Since you don't want the CategoryID in the link, there is no need to put it in. the route isn't being matched by the actionlink because it expects a CategoryID parameter as well
EDIT
If you want the CategoryID to be read from the route, it needs to be added to the route. otherwise it will just be appended as a parameter (like in your original example).
If you change your route to:
"Blog/{categoryId}/{category}"
or
"Blog/{category}/{categoryId}"
The link will now look like Blog/2/ASP.NET or Blog/ASP.NET/2 but if you want the categoryId to be read from the URL, then I don't think you have much choice

Resources