EDIT: Sorry I explained it badly. Basically, in the below example, I want "this-is-handled-by-content-controller" to be the "id", so I can grab it in ContentController as an action parameter, but I want to access it via the root of the site, e.g mysite.com/this-is-not-passed-to-homecontroller.
I'm trying to create a root route that will go to a separate controller (instead of home).
I've followed the "RootController" example posted somewhere else that implements IRouteConstraint but it just doesn't seem to work and I've already wasted a couple of hours on this!
Basically, I have a LoginController, a HomeController, and a ContentController.
I want to be able to view HomeController/Index by going to http://mysite/. I want to be able to view LoginController/Index by going to http://mysite/Login. But.. I want the ContentController/Index to be called if any other result occurs, e.g: http:/mysite/this-is-handled-by-content-controller
Is there an elegant way to do this that works?
This was my last attempt.. I've cut/pasted/copied/scratched my head so many times its a bit messy:
routes.MapRoute(
"ContentPages",
"{action}",
new { Area = "", controller = "ContentPages", action = "View", id = UrlParameter.Optional },
new RootActionConstraint()
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { Area = "", controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "Website.Controllers" }
);
Any help is appreciated greatly!
chem
I would do something similar to this, though that might not be the best solution if you keep adding more controller in the future.
routes.MapRoute(
"HomePage",
"",
new { controller = "Home", action = "Index", id="" }
);
routes.MapRoute(
"Home",
"home/{action}/{id}",
new { controller = "Home", action = "Index", id="" }
);
routes.MapRoute(
"Login",
"Login/{action}/{id}",
new { controller = "Login", action = "Index", id="" }
);
//... if you have other controller, specify the name here
routes.MapRoute(
"Content",
"{*id}",
new { controller = "Content", action = "Index", id="" }
);
The first route is for your youwebsite.com/ that call your Home->Index. The second route is for other actions on your Home Controller (yourwebsite.com/home/ACTION).
The 3rd is for your LoginController (yourwebsite.com/login/ACTION).
And the last one is for your Content->Index (yourwebsite.com/anything-that-goes-here).
public ActionResult Index(string id)
{
// id is "anything-that-goes-here
}
Assuming you have ContentController.Index(string id) to handle routes matching the constraint, this should work:
routes.MapRoute(
"ContentPages",
"{id}",
new { Area = "", controller = "Content", action = "Index" },
new { id = new RootActionConstraint() }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { Area = "", controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "Website.Controllers" }
);
Related
I want create route config multiple Url. ex: "fr-ca/user/login" and "en-ca/user/login" and its working, but if click submit and proccess data by controller and return RedirectToAction(MVC.User.Dashboard); always return "fr-ca/user/dashboard";
Although I use Url "en-ca/", and always return all link MVC.anything in first is "fr-ca/"
Because in position route config, "fr-ca" is first.
Maybe somebody can help me to solve this, thank u.
routes.MapRoute(
name: "fr-Default",
url: "fr-ca/{controller}/{action}/{id}",
defaults: new { controller = MVC.Content.Name, action = MVC.Content.ActionNames.Show, id = UrlParameter.Optional, siteId = Site.FR.Id },
namespaces: new string[] { "Jay.Cms.Web.Controllers" }
);
routes.MapRoute(
name: "en-Default",
url: "en-ca/{controller}/{action}/{id}",
defaults: new { controller = MVC.Content.Name, action = MVC.Content.ActionNames.Show, id = UrlParameter.Optional, siteId = Site.EN.Id },
namespaces: new string[] { "Jay.Cms.Web.Controllers" }
);
You can write to action attribute in controller
'[Route("fr-ca/{controller}/{action}/{id}")]
[Route("en-ca/{controller}/{action}/{id}")]
public ActionResult Home()
{
return View();
}'
For www.demo.com/city/hotel-in-city
routes.MapRoute(
name: "Search",
url: "{city}/{slug}",
defaults: new { controller = "Demo", action = "Index", city = UrlParameter.Optional}
);
For default
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
But when I call the index method of home controller www.demo.com/home/index it points to 1st route(index method of default controller).
How to handle default route ?
The problem is that your "Search" route captures basically everything. One way of handling this is to create more-specific routes for the home controller, and put those first:
routes.MapRoute(
name: "Home1",
url: "/",
defaults: new { action = "Index", controller = "Home" }
);
routes.MapRoute(
name: "Home2",
url: "Home/{action}/{id}",
defaults: new { id = UrlParameter.Optional, action = "Index", controller = "Home" }
);
routes.MapRoute(
name: "Search",
url: "{city}/{slug}",
defaults: new { controller = "Demo", action = "Index" }
);
This will filter out any URL with "Home" as the first parameter, and allow everything else through to the search.
If you have a lot of controllers, the above approach may be inconvenient. In that case, you could consider using a custom constraint to filter out either the default route, or the "Search" route, whichever one you decide to put first in the route config.
For example, the following constraint declares the match invalid, in case the routing engine has attempted to assign "Home" to the "city" parameter. You can modify this as needed, to check against all your controllers, or alternately, against a cached list of available city names:
public class SearchRouteConstraint : IRouteConstraint
{
private const string ControllerName = "Home";
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
return String.Compare(values["city"].ToString(), ControllerName, true) != 0;
}
}
This will allow URLs starting with "/Home" through to the default route:
routes.MapRoute(
name: "Search",
url: "{city}/{slug}",
defaults: new { controller = "Demo", action = "Index" },
constraints: new { city = new SearchRouteConstraint() }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional, action = "Index", controller = "Home" }
);
I have the following action result:
public ActionResult Index(int id, int? siteId)
{
//code here....
return View(object);
}
I have the following route mapping as follows:
routes.MapRoute(
name: "SettingsRoute",
url: "Settings/{id}/{siteId}",
defaults: new
{
controller = "Settings",
action = "Index",
}
);
What do I need to do, so the url will be "Settings?id=1&siteId=133" instead of the format "Settings?id=1" durring initial load. Then to select a site it builds the URl "Settings/1/133".
I am using the following actionLink to create this:
<li>#Html.ActionLink(site.Name, "Index", "Settings", new { id = Model.SettingsEnvironment.EnvironmentID, siteId = site.SiteID }, null)</li>
I can't seem to get the routing down right. Any help would be appreciated. Thanks.
You need to set your optional URL parameter:
routes.MapRoute(
name: "SettingsRoute",
url: "Settings/{id}/{siteId}",
defaults: new
{
controller = "Settings",
action = "Index",
siteId = UrlParameter.Optional
}
);
ref: http://haacked.com/archive/2010/02/12/asp-net-mvc-2-optional-url-parameters.aspx/
working on an MVC project and I'm having a tough time with rerouting my URL.
What I currently have is
http://dev.mywebsite.com/s/index?Key=abc123
which then runs the index action and completes as I'd like it to
I'd like to be able to type in
http://dev.mywebsite.com/s/abc123
and run the index action like normally
I currently have
routes.MapRoute(null, "s/index/{id}", new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
);
but I'm kind of stuck as to where to go from here. Any assistance would be greatly appreciated.
Thanks
Edit: My full routeconfig class
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("sites/{*pathInfo}");
routes.MapRoute(null, "s/index/{key}", new
{
controller = "S",
action = "Index",
key = UrlParameter.Optional
}
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Site", action = "Index", id = UrlParameter.Optional },
new[] { "Custom.Web.Controllers" }
);
}
in my controller I have the actionresult index as
public ActionResult Index(string Key)
{
return Redirect(workflow.RetrieveURL(Key));
}
So, after all our comments, the solution is:
routes.MapRoute(null, "s/{Key}",
new {
controller = "Home",
action = "Index",
Key = UrlParameter.Optional
});
Place this rule before all the others to give it preference
My Booking Controller have the following code
public ActionResult Index(string id, string name)
{
return View();
}
and my routeConfig have the below route mappings
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Search",
url: "{controller}/{location}/{checkIn}/{checkOut}/{no}",
defaults: new { controller = "Search", action = "Index", location = UrlParameter.Optional, checkIn = UrlParameter.Optional, checkOut = UrlParameter.Optional, no = UrlParameter.Optional }
);
routes.MapRoute(
name: "booking",
url: "{controller}/{action}/{id}/{name}",
defaults: new { controller = "Booking", action = "Index", id = UrlParameter.Optional, name=UrlParameter.Optional }
);}
but when I access the page http://localhost:59041/booking/index/1/libin both params returns null.
see this book
As your application becomes more complex you are likely going to
register multiple routes. When you do this its important that you
consider the order that that you register them. When the routing
engine attempts to locate a matching route, it simply enumerates the
collection of routes and it stops enumerating as soon as it find a
match.
Add a comment This can cause plenty of problems if you’re not
expecting it. Let’s look at an examples where this can be a problem:
routes.MapRoute(
> "generic", // Route name
> "{site}", // URL with parameters
> new { controller = "SiteBuilder", action = "Index" } // Parameter defaults );
>
> routes.MapRoute(
> "admin", // Route name
> "Admin", // URL with parameters
> new { controller = "Admin", action = "Index" } // Parameter defaults );
The snippet above registers two routes. The first route
contains a single placeholder segment and sets the default value of
the controller parameter to SiteBuilder. The second route contains a
single constant segment and sets the default value of the controller
parameter to Admin.
Both of these routes are completely valid, but the order in which they
are mapped may cause unexpected problems because the first route
matches just about any value entered, which means that it will be the
first to match
http://example.com/Admin and since the routing engine stops after
finding the first match, the second route would never get used.
So, be sure to keep this scenario in mind and consider the order in
which you define custom routes.
You should write booking routes at first
routes.MapRoute(
name: "booking",
url: "{controller}/{action}/{id}/{name}",
defaults: new { controller = "Booking", action = "Index", id = UrlParameter.Optional, name=UrlParameter.Optional }
);}
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Search",
url: "{controller}/{location}/{checkIn}/{checkOut}/{no}",
defaults: new { controller = "Search", action = "Index", location = UrlParameter.Optional, checkIn = UrlParameter.Optional, checkOut = UrlParameter.Optional, no = UrlParameter.Optional }
);