route :
routes.MapRoute("Default", "{controller}/{action}/{id}/{args1}/{args2}/{args3}", // URL with parameters
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional,
args1 = UrlParameter.Optional,
args2 = UrlParameter.Optional,
args3 = UrlParameter.Optional
}
);
Creating Link using below code:
#Html.ActionLink("Photos", "List", "Photos");//"photos" is controller name and "list" is action name
its generating anchor but url/link is blank.
I have modified Route as i required some extra parameters for some actions.
I am new to MVC, Please provide me solution.
Seems like default route is not active, if so then activate it.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Related
How can I replicate this default MVC route code below but to work with multiple ActionResults that are in the home controller. I want them to work just like Index where you do not need /Home/Index in the url to hit example.com/index
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I would like to hit example.com/about and example.com/contact without needing the the controller name in the beginning.
I have tried adding that same code but replaced Index with another action method and it works but it doesn't allow you to have more than 1 existing in this structure at the same time.
Solution?
Ok so I think I got it to work after reading this thread:
ASP.NET MVC - Removing controller name from URL
In the RouteConfig I added the following right before the default route:
routes.MapRoute(
"Root",
"{action}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { isMethodInHomeController = new RootRouteConstraint<HomeController>() }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Then inside of the Controller whos name you are trying to remove from the URL you need to add this:
public class RootRouteConstraint<T> : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
var rootMethodNames = typeof(T).GetMethods().Select(x => x.Name.ToLower());
return rootMethodNames.Contains(values["action"].ToString().ToLower());
}
}
Now if I go to example.com/about , it works and I don't have to use example.com/Home/About
I want to create a site where I display my apps. I use MVC 3.
My Home controller is a typical overview page where I'll list all my apps. Then there is the App controller, where if you click on a app on a overview could get more info... url scenario would be:
http://myhost.com <-- overview
http://myhost.com/App/app_name <-- render info about that app
But I'm having a small problem understanding routes, thought i did it right:
routes.MapRoute(
"App", // Route name
"{controller}/{id}", // URL with parameters
new { controller = "App", action = "Index", id = 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
);
And how do I get a hold of id in the controller?
Would your app rule not be more specific, that is
routes.MapRoute(
"App", // Route name
"App/{id}", // URL with parameters
new { controller = "App", action = "Index",
id = UrlParameter.Optional }
// Parameter defaults
);
Note replaced {controller} with App
I have 2 routes mapped in my mapping...
this is to allow a user to type in the url with an optional parameter to quick load their town in the home page of the website, example:
www.mysite.com/manchester
www.mysite.com/liverpool
or to simply go to the defaul home page if www.mysite.com is entered with nothing else.
With the default mapping in place to handle the controller/action/parameter i have added an additional route so the parameter is handed:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }); // Parameter defaults
routes.MapRoute(
"HomePageQuickFind",
"{quickFind}",
new { controller = "Home", action = "Index", quickFind = UrlParameter.Optional });
I am not very good with route mapping as I am struggling to understand it and my question is this a bad approach which my cause "greedy routing" and is there another way of implementing my scenario?
I think you need to replace the default route with a set of routes for each of your controllers, then add your quick find route as the last route. This should allow any unmatched routes to fall through to the quick find route. Try something like this:
// Routes for standard controllers
routes.MapRoute(
"Home",
"home/{action}/{id}",
new { controller = "home", action = "index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Map",
"map/{action}/{id}",
new { controller = "map", action = "index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default",
"users/{action}/{id}",
new { controller = "users", action = "index", id = UrlParameter.Optional }
);
// Route for www.mysite.com/cityname
routes.MapRoute(
"QuickFind",
"{quickFind}",
new { controller = "home", action = "index", quickFind = UrlParameter.Optional }
);
In my global.asax I have three routes:
//MemberHome is supposed to handle urls like http://localhost/johndoe
routes.MapRoute(
"MemberHome", // Route name
"{username}",
new { controller = "PublicMember", action = "Index", username = "username" }
);
//Home is supposed to catch http://localhost/
routes.MapRoute(
"Home",
""
);
// the default way of doing things..
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The problem is, I can get them to work separtely (just home or just MemberHome)
But when they are both activated its either Home or MemberHome which gives me a 404 resource not found..
Any idea how I can get this work?
You need to set the first part of the route to something distinct. Eg:
routes.MapRoute(
"MemberHome", // Route name
"MemberHome/{username}",
new { controller = "PublicMember", action = "Index", username = "username" }
);
or:
routes.MapRoute(
"Default",
"Home/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
That way they will be distinct. Whichever one is given the fixed part of the route should come first, as otherwise the route handler will match the wildcard one first...
I have Action called Contact in home controller
<mysite>/Home/Contact
I want to be able by typing <mysite>/Contact to get the same result as <mysite>/Home/Contact
Is it possible to do with mvc 3.0 routes or RouteMagic?
Currently i am trying to achieve this like that, but no luck:
Custom Routes:
routes.MapRoute(
"Contact", // Route name
"Contact", // URL with parameters
new { controller = "Home", action = "Contact", id = UrlParameter.Optional } // Parameter defaults
);
RouteMagic:
var route = routes.MapRoute("new", "Contact");
routes.Redirect(r => r.MapRoute("old", "Home/Contact"))
.To(route);
Update
Ok the custom routes should be defined first, now it is working(in case of custom routes), but there is appeared a new question why route magic returning error:
Server Error in '/' Application.
Value cannot be null or empty.
Parameter name: controllerName
Make sure your new route occurs before the default route (since it will match as well) when defining the route.
routes.MapRoute(
"Contact", // Route name
"contact", // URL with parameters
new { controller = "Home", action = "Contact", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Contact", id = UrlParameter.Optional } // Parameter defaults
);
have you tried the rewrite module in iis7 ?
its easy to use , donwlod it from here:
http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/