I have one area Area1 with controller Home and it's Index method.
I also have created another area Area2 with controller Home and it's Index method.
In Area1 I have action link that should open Index page in Area2.
#Html.ActionLink("Link to another area index", "Index", "Home", new { area = "Area2" }, null)
But when I click on this link it first go to Area1/Home/Index!
Why is this happening. Is this has to be like this or it can go directly to Area2/Home/Index?
This makes me problem because in Area1/Home/Index I need some parameters and when this happens this valuer are null or wrong and make me problem. I must doing something wrong.
Update
Area1 routing
public override void RegisterArea(AreaRegistrationContext context)
{
context.Routes.IgnoreRoute("{*favicon}", new { favicon = #"(.*/)?favicon.ico(/.*)?" });
context.MapRoute(
"Area1_home",
"{country}/{city}",
new { controller = "Home", action = "Index", country = UrlParameter.Optional, city = UrlParameter.Optional }
);
context.MapRoute(
"Area1_default",
"Area1/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Area2 routing:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Area2_default",
"Area2/{controller}/{action}/{id}",
new { controller="Home", action = "Index", id = UrlParameter.Optional }
);
}
Try:
Html.ActionLink("Link to another area index", "Index", "Home", new { Area = "Area2" }, new{});
Related
How would I create a route mapping to the following url:
http://localhost/SiteName/AdminCP/Topics/EditTopic/28
AdminCP is an area -- I can get to the Topics controller and show a list of topics to the user, then the user clicks a link to edit the topic which should
take them to the url above. EditTopic is a controller in AdminCP which returns a single Index action.
This is my AdminCP registration code which is not working.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"AdminCP_default",
"AdminCP/{controller}/{action}/{id}",
new { controller= "Home", action = "Index", id = UrlParameter.Optional }
);
context.MapRoute(
"AdminCP_Topics",
"AdminCP/Topics/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
I have been able to reproduce this URL:
http://localhost/SiteName/AdminCP/EditTopic/Index/26
using this code snippet
#Html.ActionLink("Edit Options", "", "EditTopic", new { id = item.CategoryId }, new { #class = "popup-link" })
...but that's not exactly what I want.
Try this (Add before the AdminCP Default Route)
context.MapRoute(
"AdminCP_Topics",
url: "AdminCP/Topics/{controller}/{id}",
defaults: new { action = "Index", id = UrlParameter.Optional }
);
And
#Html.ActionLink("Edit Options","Index","EditTopic", new {id = 1, area = "AdminCP" },null)
I use RedirectToRoute method to force culture in the url. All was working until I create an Admin area in my project. Now the method redirects to the Area controller.
Example with the main HomeController : /Home/Contact
public ActionResult Contact()
{
Response.RedirectToRoute(RouteData.Values);
return View();
}
The method redirects to /Admin/Home/Contact, the values of RouteData.Values before the redirection are :
[0] "Controller" Home
[1] " Action" Contact
My Main Route :
routes.MapRoute("Default_culture", "{culture}/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional , new { culture = "([a-z]{2,3})(-[a-zA-Z]{2})?" }, new[] { "Project.Controllers" });
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new[] { "Project.Controllers" });
Route in RegisterArea method :
context.MapRoute(null, "{culture}/Admin/{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new { culture = "([a-z]{2,3})(-[a-zA-Z]{2})?" }, new[] { "Project.Areas.Admin.Controllers" });
context.MapRoute(null, "Admin/{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new[] { "Project.Areas.Admin.Controllers" });
I don't understand this behaviour. What am I doing wrong?
I just fixed this problem in my site.
Change Response.RedirectToRoute(RouteData.Values);
To Response.RedirectToRoute("Default", RouteData.Values);
The problem I'm facing is that I have 2 controllers with the same name. One in the main controller folder, and the other in the controller folder in my Admin Area.
Calling the action result directly works fine:
MySite/Admin/Account/GetAccount?userId=1
Calling through the route doesn't work
MySite/Admin/User/1/Account
Any idea What I'm doing wrong?
Application_Start
AreaRegistration.RegisterAllAreas()
RouteConfig
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
new[] { "MyCompany.Controllers" }
);
}
AdminAreaRegistration
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
context.MapRoute(
"GetUserAccount",
"Admin/User/{userId}/Account",
new { controller = "Account", action = "GetAccount" },
new[] { "MyCompany.Areas.Admin.Controllers" }
);
}
My Action Result In Areas/Admin/AccountController
public ActionResult GetAccount(string userId)
{
// return Account Type
}
i think you should change the positions of the account and check again
context.MapRoute(
"GetUserAccount",
"Admin/User/{userId}/Account",
new { controller = "Account", action = "GetAccount" },
new[] { "MyCompany.Areas.Admin.Controllers" }
);
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
please I need help with this scenario:
I have an Area "\UI" with one Controller "ActionController" and his View "Login.cshtml" also in his UIAreaRegistration class:
public class UIAreaRegistration : AreaRegistration
{
public override string AreaName
{
get { return "UI"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"UI_default",
"UI/{controller}/{action}/{id}",
new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
}
}
Now I want Login.cshtml to be the first view of the app.
Then in 'RegisterRoutes' of global.asax.cs I have:
AreaRegistration.RegisterAllAreas();
//Default
//routes.MapRoute(
// name: "Default",
// url: "{controller}/{action}/{id}",
// namespaces: new[] { "", "" },
// defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
//);
routes.MapRoute(
name: "Default",
url: "{area}/{controller}/{action}/{id}",
defaults: new { area = "UI", controller = "Account", action = "Login", id = UrlParameter.Optional }
);
But didn't work, please what should I do?
Thanks in advance
What you are trying to do is to make your UI area a default area, so any controller in UI will be accessible without UI prefix in URL.
The problem with that is that the controllers which are in root folder will no longer be accessible. If it is what you want you can do it by changing the area route registration from :
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"UI_default",
"UI/{controller}/{action}/{id}",
new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
}
to:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"UI_default",
"{controller}/{action}/{id}",
new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
}
Easier solution will be to create an action on the default controller which will redirect user to your area
Maybe I don't understand real purpose of asp mvc routing.
I created an application and now I need to fix my url's a to be more understandable.
For example I have area Cities with controller Home and action Index.
So here I need url like: localhost/London but with current routing I get localhost/cityPage/Home.
My question is can I somehow pass parameter like city name and make URL like I want?
This is my current default routing in Global.asax
routes.MapRoute(
"Default",
"{area}/{controller}/{action}/{id}",
new { area = "CityPage", controller = "Home", action = "Index", id = "" },
new string[] { "MyProject.Areas.Cities.Controllers" }).DataTokens.Add("area", "Cities");
New routing:
routes.MapRoute(null,
"CityPage/{cityName}",
new
{
area = "CityPage",
controller = "Home",
action = "Index"
}
);
routes.MapRoute(
"Default",
"{area}/{controller}/{action}/{id}",
new { area = "CityPage", controller = "Home", action = "Index", id = "" },
new string[] { "MyProject.WebUI.Areas.CityPage.Controllers" }).DataTokens.Add("area", "CityPage");
Example of link that I click
#Html.ActionLink("City London", "Index", "Home", new { cityName = "London" }, null)
In order to route the URL localhost/London to the Index action on the HomeController of the Cities area, you need a route like this:
routes.MapRoute(null,
"{id}",
new
{
area = "Cities", controller = "Home", action = "Index"
}
);
Be sure this route is declared before the "Default" route in your CitiesAreaRegistration.cs class.
However if you have a lot of other routes in your application, adding a general route like this can play havoc with other routes in the app. I suggest adding a URL prefix to separate this route from others in your application:
routes.MapRoute(null,
"cities/{id}",
new
{
area = "Cities", controller = "Home", action = "Index"
}
);
This will make your URL look like localhost/cities/London. Is that acceptable?
Update 1
Unless you completely remove your "Default" route definition, you will actually have multiple INBOUND routes that map to this action. You would have localhost/cities/London, localhost/cityPage/Home, localhost/cityPage/Home/Index, and localhost/cityPage/Home/Index/London all resolving to that action. However when MVC chooses to generate an OUTBOUND route, it will choose the first one -- localhost/cities/London.
Update 2
If you want your route parameter to be cityName, you would do this:
routes.MapRoute(null,
"cities/{cityName}",
new
{
area = "Cities", controller = "Home", action = "Index"
}
);
However you would then have to change the Index action on your Cities area's HomeController to have this signature:
public ActionResult Index(string cityName)
By changing the argument from id to cityName, you are telling MVC to pass this URL paramter / route segment to the action method.
Update 3
Is the name of your area "Cities" or "CityPage"? From previous code it looked like the name of your area was Cities.
If it is CitiesPage, try this for your action method:
#Html.ActionLink("City London", "Index", "Home",
new { area = "CityPage", cityName = "London" })
Final Answer
I just reproduced this in an MVC3 project, and it is working as expected:
Created a new area named "CityPage"
Added a HomeController with an Index action to the CityPage area
Added an Index view to the CityPage/Views/Home folder.
CityPageAreaRegistration.cs:
public class CityPageAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "CityPage";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(null,
"CityPage/{cityName}",
new { area = "CityPage", controller = "Home", action = "Index" }
);
//context.MapRoute(
// "CityPage_default",
// "CityPage/{controller}/{action}/{id}",
// new { action = "Index", id = UrlParameter.Optional }
//);
}
}
HomeController.cs:
public class HomeController : Controller
{
//
// GET: /CityPage/Home/
public ActionResult Index(string cityName)
{
return View();
}
}
Index.cshtml:
#{
ViewBag.Title = "Index";
}
<h2>
Index</h2>
#Html.ActionLink("City London", "Index", "Home",
new { area = "CityPage", cityName = "London" }, null)
Finally, here is the link generated by the action link:
City London
yes you can do this way but you have to do following thing
Make sure your route must register before generic route.
Get Information about RouteConstraint
http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-route-constraint-cs
http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-custom-route-constraint-cs
Just for example Try this way check your required url localhost/London
routes.MapRoute(
"Default",
"{id}",
new { area = "CityPage", controller = "Home", action = "Index", id = "" },
new string[] { "MyProject.Areas.Cities.Controllers" }).DataTokens.Add("area", "Cities");