In my website, I have the following default route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
When I access the Index page from the Home controller, I get the following address:
http://localhost/MyWebsite/
Everything is okay, however, I would like to add another default route for the following Controller and Page:
http://localhost/MyWebsite/Profile/Index/8
For the link above, I would like to have the following route:
http://localhost/MyWebsite/Profile/8
Without showing the "Index" page name.
How is it possible?
Inside the RouteConfig, set enable to route Actions via Attribute:
routes.MapMvcAttributeRoutes();
After that, add the Attribute Route above the Action name:
[Route("Perfil/{id}")]
public ActionResult Index(int? id)
{
return View();
}
Related
I couldn't get any good information on how to define a route to a contrete action in the selected controller. MSDN doesn't provide clean information on this. There is a mention of action parameter but it doesn't seem to be working.
What I want to achive is to route path like /vehicles/check*** to Check method in the VehiclesController.
A concrete invokation is /vehicles/check?licencePlate=XYZ =>
VehiclesController -> Check(string licenePlate)
I have this map but it does not work:
config.Routes.MapHttpRoute("VehicleTransactions", "Vehicles/Check",
new { controller = "Vehicles", action= "Check" });
Can it be done with MVC?
Thanks, Radek
MapHttpRoute is used for mapping Web API routes. For MVC Controller routes we tend to use MapRoute
So Assuming
public class VehiclesController : Controller {
public ActionResult Check(string licenePlate) {
//...
return View();
}
}
The route would be mapped to
routes.MapRoute(
name: "VehicleTransactions",
url: "vehicles/check",
defaults: new { controller = "Vehicles", action= "Check" }
);
Try this:
routes.MapRoute(
name: "VehicleTransactions",
url: "Vehicles/Check/{licencePlate}",
defaults: new { controller = "Vehicles", action = "Check", licencePlate = UrlParameter.Optional });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Set "VehicleTransactions" routing before of "Default" routing, because otherwise "Default" overwrite the other.
Special routes is set always before default routing.
In ASP.NET MVC route configuration is like below:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I am using [Route] attribute in Controller:
[Route("login")]
public ActionResult Login()
{
return View();
}
I can go to the page using both /login and Auth/Login. The second link is clearly from the default route, but I don't want that URL to go to my login page.
How can I do that?
To block a route from being served, you can use IgnoreRoute as long as you place it before the route you want to block:
routes.IgnoreRoute(url: "Auth/Login");
// Register any custom routes here...
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I have a URL request in the following format: http://localhost/activate?activationCode=X
I would like this request to be handled in the Home controller, by the Activate action.
I am not sure how to proceed. I have looked at RouteConfig.cs and see the way routes are defined here:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
But how can I make Home/Activate handle URL's of the following format? http://localhost/activate?activationCode=X
...so as to add a special case where activate proceeding the host name goes to the Home controller, and Activate action?
You need to create a specific route
routes.MapRoute(
name: "Activate",
url: "Activate/{id}",
defaults: new { controller = "Home", action = "Activate", id = UrlParameter.Optional }
);
and place this one before the default route.
In addition if you want http://localhost/activate/X rather than http://localhost/activate?activationCode=X, then change it to
routes.MapRoute(
name: "Activate",
url: "Activate/{activationCode}",
defaults: new { controller = "Home", action = "Activate", activationCode = UrlParameter.Optional }
);
and make the method in HomeController
public ActionResult Activate(string activationCode)
I am having problems setting up the RouteConfig file to accept an optional record ID as part of the URL like in the examples below.
http://localhost/123 (used while debugging locally)
or even
http://www.foobar.com/123
Ideally, I would like to have the record ID (123 as in the examples above) be passed in as a parameter to the Index view of the Home controller. I had thought that the default routeconfig would suffice for this (using the ID as an optional element), but apparently the application is apparently trying to direct the browser to a view called '123' which obviously doesn't exist.
My current RouteConfig.cs looks like this:
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 }
);
}
Any assistance on this would be greatly appreciated.
Your routing says:
{controller}/{action}/{id}
that's your site then your controller, then your action and then an id.
You want just site then id:
routes.MapRoute(
name: "Default",
url: "{id}",
defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional }
);
This would then hit a controller at
public class DefaultController : Controller
{
public ActionResult Index(int id)
{
}
}
"{controller}/{action}/{id}" tells MVC that a route may have a controller name, or it may have a controller name followed by an action name, or it may have those two followed by an ID. There's no way, given just an ID, for the routing to understand that it's supposed to be an ID and not an action or a controller name.
If you're never planning to have any other controllers or actions, something like this might work:
routes.MapRoute(
name: "Default",
url: "{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
But that's probably a bad idea. Pretty much every site has at least one key word to indicate what the ID represents. For example, StackOverflow has "/questions/{id}".
I have a very simple normal route and I can't seem to get it to work. I'm kind of clueless what I'm missing.
My routing is:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
My Controller is called AccountController and it has this method:
public virtual ApplicationUser EditUser(String userId)
I post this URL and I get a userid that is null
/Account/EditUser/Patrick
What am I missing?
Your route has a parameter called "id", whereas your method has a parameter called "userId". These need to match.
So either create a route, like:
routes.MapRoute(
name: "EditUser",
url: "Account/EditUser/{userId}",
defaults: new { controller = "Account", action = "EditUser"});
Or change your method to be:
public virtual ApplicationUser EditUser(string id);
Note that if you choose the first option, you need to put that call before the existing default one, because any URL you enter will match against the first route which matches it.