I have an AppController and an AccountController. The AppController only has one view, index, which takes query string parameters from the id part of the url.
The default route is as follows: {controller}/{action}/{id}
This means for the query string parameters to work properly, the view name has to be in the url. url/view/id
I would like to hide that part of the url and render that view by default, so users need only go to url/id.
I have tried {controller}/{id} and {controller}/index/{id} but neither work.
I think this would work. Set the url as : "{controller}/{id}" and give it a default action parameter:
routes.MapRoute(
name: "Default",
url: "{controller}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Related
I have an app that is just one controller and one action, but I want to pass two values into that action. The end result that I'm looking for is a url that looks like this http://www.example.com/parameter1/parameter2
So I was thinking that the routing would look like this
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{name}",
defaults: new { controller = "Home", action = "Index"}
);
and the controller would look like this
public class HomeController : Controller
{
public ActionResult Index(string id, string name)
{
return View();
}
}
But I'm clearly wrong as it doesn't work. Does anyone know if it's possible under the index action?
Just to clarify, I want 2 parameters in the default action. I'm aware it's possible by having something like http://www.example.com/books/parameter1/parameter2/ but I specifically want http://www.example.com/parameter1/parameter2/
To totally omit the controller and action placeholders in the route you can just remove them.
(Do not remove it from your default route, better create a new one and place it about the default one)
routes.MapRoute(
name: "Default",
url: "{id}/{name}",
defaults: new { controller = "Home", action = "Index"}
);
This route will only work with Index action from HomeController but not with others.
If id is optional, what your URL would look like when it's not entered, but name is?
/Home/Index//name
That's obviously invalid.
Consider using the values in the query string instead of part of the URL.
I used this and this to solve the problem.
Need two Routes and make sure these routes come above the default MVC route:
routes.MapRoute(
name: "with-name",
url: "Home/{action}/{id}/{name}",
defaults: new { controller = "Home", action = "Index"}
//No Optional
);
routes.MapRoute(
name: "without-name",
url: "Home/{action}/{id}",
defaults: new { controller = "Home", action = "Index"}
);
I have a controller called CarController located in a folder called Buy. So the url becomes www.website.com/Buy/Car
How do I make the url be instead "/purchase/vehicle" without changing controller and folder name?
Thanks!
You need to define a new route for it
routes.MapRoute(
name: "VehicleRoute",
url: "purchase/vehicle",
defaults: new { controller = "Car", action = "TheAction" }
);
Just make sure you have placed it before the default route.
You can do this with a custom route. See here for informations about routing. You can then create your custom route with default values for the Controller and the action, something like:
routes.MapRoute(
"MyRoute",
"purchase/vehicle",
defaults: new { controller = "Car", action = "Buy" }
);
You have to put in there the correct controller name and the action you want to call.
there's
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
I want to make my url to allow enter company name in the beginning, e.g.:
url: "{company}/{controller}/{action}/{id}"
So I could navigate through pages and now that the base is some company.
domain.com/company-ltd
domain.com/company-ltd/products
domain.com/company-ltd/edit
domain.com/some-other-company-name-ltd/products
and so on.
How could I achieve this? Thanks.
The routes work off the back of patterns; a URL coming in is matched against patterns until one is found that it matches.
Assuming that you're not talking about optional company names, you should be able to get away with:
routes.MapRoute(
name: "Default With company",
url: "{company}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional
);
and then simply ensure that all methods take a company parameter. However, this would fail on more simple routes, so make sure you set a default for pages such as the root. If you do end up wanting additional routes that don't involve a company, those can also be catered for with controller-specific routes:
routes.MapRoute(
name: "Profile",
url: "profile/{action}/{id}",
defaults: new { controller = "Profile", action = "Index", id = UrlParameter.Optional
);
before the less-specific company one.
Note that you can test these routes quite easily in .net using NUnit and MvcContrib.TestHelper, for example:
"~/company/".WithMethod(HttpVerbs.Get)
.ShouldMapTo<Controller.HomeController>(x =>
x.Index("company"));
"~/company/products/".WithMethod(HttpVerbs.Get)
.ShouldMapTo<Controller.ProductsController>(x =>
x.Index("company"));
to ensure that they're going to the locations you'd expect.
Pretty basic question here. How come I get a 404 error when I try to view
/Vendors/123
But I can see the page if I go to
/Vendors/Index/123
Is there any way for me to make it work the first way? I'm just using the default routes being registered in Global.asax.
Because that's how the default route is defined. If you don't want to specify an actionname you could modify it like this:
routes.MapRoute(
"Default",
"{controller}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
This being said you probably shouldn't be doing this because now in your route you do not have the possibility to specify the action name that you wish to invoke on a given controller which means that you can only have a single action on each controller and that action would be Index. On the other hand if you wanted to do this on some specific controller you could still preserve the default route and add another one:
routes.MapRoute(
"Vendors",
"vendors/{id}",
new { controller = "Vendors", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Because in your basic routing {id} expects after action name, but you are trying to put it after controller name.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Change your rout to accept {id} just after {controller}. Just like #Darin described.
Could someone show me how to use the MapRoute method? I have tried creating my own routes, but it's not working. What i want to accomplish is a route that routes "http://servername/home/default.aspx" into controller "Home" and action "Default". Also, would it be possible to say that if the user is browsing the default.aspx "file", it would actually point to the "Index" action?
I have tried reading the MSDN references and googling, but it didn't make me any wiser.
Probably too late to help the developer who raised the question but may help someone else. New to MVC but what I found is the map routes seem to be processed in the order they are added. I had a similar problem, my specific route was not working until I started adding the default route as the last route.
If the default map route is added before your custom one and your custom URL matches the structure defined by the default map route you will never reach your custom route.
The route you want to configure the first part of your question is:
routes.MapRoute(
"",
"home/default.aspx",
new { controller = "Home", action = "Default" }
);
Assuming you wish to 'browse' default.aspx with some sort of parameter you can do something like:
routes.MapRoute(
"",
"home/default.aspx/{param}",
new { controller = "Home", action = "Default", param = UrlParameter.Optional }
);
And you would then need to create your Default action to accept string param.
You also have to make sure the parameter name is the same as the action's parameter name.
Example:
routes.MapRoute(
name: "MyName",
url: "{controller}/{action}/{myParam}",
defaults: new { controller = "MyController", action = "MyAction", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
MyController:
public ActionResult MyAction(string myParam = "")
{
}