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)
Related
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.
I have next specific map routes
routes.MapRoute(
"MyPagePost",
"URL-Up/{name}",
new { controller = "MyController", action = "MyPostAction" },
new { httpMethod = new HttpMethodConstraint("POST") }
);
routes.MapRoute(
"MyPageGet",
"URL-Up/{name}",
new { controller = "MyController", action = "MyGetAction" },
new { name = "[A-Za-z].+", httpMethod = new HttpMethodConstraint("GET") }
);
my default controller looks like
routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", culture = "en", id = UrlParameter.Optional },
constraints: new { culture = #"[a-zA-Z]{2}" }
);
and the issue is next:
my get MyPageGet route show a page with include FORM with POST reqvest to MyPagePost route, but on a first call I am getting the same GET request and see in URL other extra param ?culture=de. Moreover, with or without this parameter, second call it working fine via MyPagePost route.
UPDATE:
In Chrome or fiddler Logs I see that reqvest to URL-Up/Bla-Bla has 302 status and response heared is URL-Up/Bla-Bla?culture=de. Why it can't be processed ?
just try it with
#using(Html.BeginRouteForm("MyPagePost",FormMethod.Post))
{
<input type="submit" value="Submit"/>
}
The routes in your post working for me in both html.beginform and html.beginrouteform on the first time.
i try it with the following routes and action methods
routes.MapRoute(
"MyPagePost",
"URL-Up/{name}",
new { controller = "Home", action = "PostAction" },
new { name="[A-Za-z].+", httpMethod = new HttpMethodConstraint("POST") }
);
routes.MapRoute(
"MyPageGet",
"URL-Up/{name}",
new { controller = "Home", action = "GetAction" },
new { name = "[A-Za-z].+", httpMethod = new HttpMethodConstraint("GET") }
);
routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", culture = "en", id = UrlParameter.Optional },
constraints: new { culture = #"[a-zA-Z]{2}" }
);
public ActionResult GetAction()
{
return View();
}
[HttpPost]
public ActionResult PostAction()
{
return View();
}
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);
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{});
I have an ASP.Net MVC 3 application. With 2 Areas:
Auth - handles all the authentication etc
Management - area for property management
In the Management Area I have a ManagementController and a PropertyController. The ManagementController does nothing, it only has a simple Index ActionResult than return a view.
The PropertyController has an Index ActionResult that returns a view with a list of properties as well as an Edit ActionResult that takes a propertyId as parameter. In the Property Index view i have a grid with the list of properties and an option to edit the property with the following code:
#Html.ActionLink("Edit", "Edit","Property", new { id = item.PropertyId })
In theory this should redirect to the Edit ActionResult of my Property Controller,however it redirects to the Index ActionResult of my ManagementController. My ManagementAreaRegistration file looks like this:
context.MapRoute(null, "management", new { controller = "management", action = "Index" });
context.MapRoute(null, "management/properties", new { controller = "Property", action = "Index" });
context.MapRoute(null, "management/properties/Edit/{propertyId}", new { controller = "Property", action = "Edit" });
And my global.asax's RegisterRoutes like this:
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
);
What am I missing, why would it redirect to the wrong controller and action?
Thanks in Advance!
You might need to specify the area in your route values parameter:
#Html.ActionLink("Edit", "Edit", "Property", new { id = item.PropertyID, area = "Management" }, new { })
Based on the constructor used for this call, you need to specify the last parameter, htmlAttributes, which, for your purposes, would be empty.
In your route you defined a parameter called propertyId not id:
context.MapRoute(null, "management/properties/Edit/{propertyId}", new { controller = "Property", action = "Edit" });
Try this:
#Html.ActionLink("Edit", "Edit","Property", new { propertyId = item.PropertyId })
I'd suggest using constraints.
For example my default route in the Global.asax is as follows:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { typeof(MyProject.Controllers.HomeController).Namespace });
Then in my area registration:
context.MapRoute(
"Search_default",
"Search/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { typeof(MyProject.Areas.Search.Controllers.HomeController).Namespace }
);
This means I get {AppName}/ as well as {AppName}/Search/Home and both work a treat.
From any Area to Home do following
Url.Action("Details", "User", new { #id = entityId, area = "" })
From Home to any Area
Url.Action("Details", "Order", new { #id = entityId, area = "Admin" })