Can I set up a route that will get mapped from a root-level URL like this?
http://localhost:49658/
I'm using the VS2010 built-in web server.
Attempting to set up a route with a blank or a single-slash URL string doesn't work:
routes.MapRoute(
"Default",
"/",
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
It results in the error "The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character.". Thanks in advance! My entire route definition is here:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"EditingTitles", // Route name
"{controller}/{action}/{startingLetter}", // URL with parameters
new { controller = "Admin", action = "Index", startingLetter = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
What are you trying to achieve here... a URL that looks like this? http://www.acme.com/ ? Because if you are, the default route will achieve that when none of the parameters are specified.
// Default Route:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = String.Empty } // Parameter defaults
);
Using ASPNET MVC5:
RouteConfig.cs file:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Homepage",
url: "",
defaults: new { controller = "Content", action = "Index" }
);
routes.MapRoute(
name: "foo",
url: "bar",
defaults: new { controller = "Content", action = "Index" }
);
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{title}",
defaults: new { controller = "Content", action = "Details", title = UrlParameter.Optional }
);
}
Plus:
If you wishes to redirect your homepage to another route automatically, like "http://www.yoursite.com/" to "http://www.yoursite.com/bar", just use the method RedirectToRoute():
public class ContentController : Controller
{
public ActionResult Index()
{
return RedirectToRoute("foo");
}
}
Related
I am creating a friendly URL so i have crete route in routeconfig file as follows
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Auth", action = "LogIn", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "ShowUser", // Route name
url: "{controller}/{action}/{id}", // URL with parameters
defaults: new { controller = "UserGroupEdit", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
and action link as follows
View
But it is not working giving runtime error "The resource cannot be found."
Default route and you custom route have identical structure, so default one will always be used because it is declared first.
Im guessing you are getting error because in code you are referring to ShowUser, but instead you should use name of the controller: UserGroupEdit.
View
If you want a friendly route you can do something like this
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "ShowUser", // Route name
url: "ShowUser/index/{id}", // URL with parameters
defaults: new { controller = "UserGroupEdit", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Auth", action = "LogIn", id = UrlParameter.Optional }
);
}
in your cshtml
#Url.Action( "Index", "UserGroupEdit")
You can use UrlHelper.RouteUrl helper method which allows you to specify the route name:
View
RouteConfig:
routes.MapRoute(
name: "ShowUser",
url: "ShowUser/{action}/{id}",
defaults: new { controller = "UserGroupEdit", action = "Index", id = UrlParameter.Optional }
);
Reference link
I need this url http://localhost:53249/Admin/EditPosts/1/Edit but unfortunately i m getting query string in url like this http://localhost:53249/Admin/EditPosts?id=1&operation=Edit
This is my actionlink(anchor tag)
<td>#Html.ActionLink(#posts.Title, "EditPosts", "Admin", new { id = posts.id, operation="Detail" }, null)</td>
This is my route config:
public static void RegisterRoutes(RouteCollection routes)
{
//routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Admin",
url: "Admin/EditPosts/{id}/{operation}",
defaults: new { controller = "Admin", action = "EditPosts"}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
In this case you are mixing Action and Operation.
I suggest that you use the url like this:
http://localhost:53249/Admin/Posts/1/Edit
Because this way you already indicate your action, Edit, for the Posts objects, and Edit is the action, not an operation, following standard REST.
To use the url suggested you must change the MapRoute to:
routes.MapRoute(
name: "Admin",
url: "Admin/Posts/{id}/{action}",
defaults: new { controller = "Posts", action = "Details" } //Details action by default
);
I have a Login controller with an Index view that I want rendered at the url http://sitename/login. It works with I browse to http://sitename/login/index but I want to omit index from the URL. Here's my route config (default):
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Dashboards", action = "Dashboard_1", id = UrlParameter.Optional }
);
}
.. and the controller:
public class LoginController : Controller
{
// GET: Login
public ActionResult Index()
{
return View();
}
}
If you had left the Default route alone, it would do what you ask. You just have to set the default value for the action route value as Index.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Alternatively, you can insert an additional route to cover your Login controller.
routes.MapRoute(
name: "Login",
url: "login/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Dashboards", action = "Dashboard_1", id = UrlParameter.Optional }
);
A third option is to rename your LoginController.Index method to LoginController.Dashboard_1, since that is your default action name for every controller in the application.
I have created multiple routes with different route name in MVC.
routes.MapRoute(
name: "PostDetails",
url: "Ad/{id}/{item}",
defaults: new { controller = "Home", action = "Post" }
);
I am calling route from a javascript function to redirect to this route
var url = '#Url.RouteUrl("PostDetails", new { id = "_id_", item = "_name_" })';
url = url.replace("_id_", id).replace("_name_", name);
window.location.href = url;
This is giving an error with 404.
You must add your route before Default route in the RouteConfig like:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "PostDetails",
url: "Ad/{id}/{item}",
defaults: new { controller = "Home", action = "Post" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
I have a controller named Registration and an action method in it as the following:
public JsonResult GetReqs(GridSettings gridSettings, int rt)
{
...//whatever
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
So I added a route and now my RouteConfig.cs is like this:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "RegReq",
url: "{Registration}/{GetReqs}/{rt}",
defaults: new { controller = "Registration", action = "GetReqs", rt = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
However I can't access the rt parameter and the action method GetReqs isn't called(I set a breakpoint on it but nothing happened). Where is the mistake?
Edit: Link example I tried : ~/Registration/GetReqs/1
I think you need to remove the brackets in your first route:
routes.MapRoute(
name: "RegReq",
url: "Registration/GetReqs/{rt}",
defaults: new { controller = "Registration", action = "GetReqs",
rt = UrlParameter.Optional }
);
The default route has {controller} to tell MVC to use the string in that section of the url as the controller name. You know the controller, so you just need to match the specific string.