Different url from controllername in asp.net MVC - c#

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.

Related

Passing multiple parameters to default action

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"}
);

How to call MVC routes from the View

I have the problem where URL's are being added on the current URL for example:
Localhost/Config -> Localhost/Config/Profile
By making the call #Html.ActionLink(Resources.General.Achievements, "Index", "Profile")
When what I really want is: Localhost/Profile
I know this is possible using MVC Routes, I just can't figure out how to call them from the View. I have used Html.BeginRouteForm, but this route won't be triggered on submit.
This is my route config
routes.MapRoute(
name: "Profile",
url: "Profile/{id}",
defaults: new { controller = "Profile", action = "Index" },
namespaces: new[] { "Cobalt.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Cobalt.Controllers" }
);
I am trying to access the Profile controller and Index action in the root directory. I have both Config and Manage Areas, which I am trying to escape from when making calls back to controllers in the root directory.
Thanks in advance for any help
When dealing with areas, if you want to move from one area to another area or out of an area into the root, you can use the overload of ActionLink which allows you to specify route data and pass an empty string (or the appropriate area name) into area:
#Html.ActionLink(Resources.General.Achievements, "Index", "Profile", new { area = string.Empty })
Use RouteLink:
#Html.RouteLink(Resources.General.Achievements, "Profile", new { id = "1234" })
Do note that since your "id" parameter is not declared optional on your Profile route it won't match unless you supply it.

Can I hide a part of a route?

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 }
);

Understanding ASP.NET MVC Routes reg

I've created a system in MVC using the NerdDinner tutorial as a base to work off.
Everything was working fine until I used single action methods such as
Here is the global.asax.cs
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "mysample", id=UrlParameter.Optional }
);
which routes to
http:localhost/Home/mysample
i just want to create routes which has more than one action in the sense
http:localhost/<controller>/<action>/<params>
ex: localhost/mycontroller/myaction/details/myname
Any help much appreciated.
Thanks.
update 1:
i have writen router like this as
routes.MapRoute(
"myname", // Route name
"{controller}/{action}/{details}/{myname}", // URL with parameters
new { controller = "mycontroller", action = "myaction", details= "details", myname= "" } // Parameter defaults
);
and retried the value with following syntax as
String name=RouteData.Values["myname"].ToString();
it works fine .
but even though the url called as
localhost/mycontroller/myaction/details
its being routed to that controller and error is being thrown as null reference...
how to avoid it?
You can't define multiple actions in one MVC route.
In MVC routing configuration is used for mapping your Controlers and Actions to user friendly routes and:
Keep URLs clean
Keep URLs discoverable by end-users
Avoid Database IDs in URL
Understanding default route config:
routes.MapRoute(
name: "Default", // Route name
routeTemplate: "{controller}/{action}/{id}", // URL with parameters
defaults: new { controller = "Home", action = "mysample", id=UrlParameter.Optional }
);
The "routeTemplate" property on the Route class defines the Url
matching rule that should be used to evaluate if a route rule applies
to a particular incoming request.
The "defaults" property on the Route class defines a dictionary of
default values to use in the event that the incoming URL doesn't
include one of the parameter values specified.
Default route will map all requests, because it has defined default values for every property in routeTemplate, {} means that property is variable, if you not provide value for that param in URL, it will try to take default value if you provide it. In default route it has defined defaults for controller, action and id param is optional. That means if you have route like this:
.../Account/Login
It will take you to Account controller, Login action and because you didn't specified prop and it is defined as optional it will work.
.../Home
This will also work, and it will take you to Home contoller and mysample action
When you define custom route, like you did:
routes.MapRoute(
"myname", // Route name
"{controller}/{action}/{details}/{myname}", // URL with parameters
new { controller = "mycontroller", action = "myaction", details= "details", myname= "" } // Parameter defaults
);
You didn't specified myname as optional and you didn't specified it your route, that means that your URL: localhost/mycontroller/myaction/details wan't be handled by your custom route myname. It will be handled by default route. And when you try to access your myname param in controller it wan't be there and you will get null reference error. If you want to specifie default value of your parameter if not present in url you need to do that in your controller. For example:
public class MyController : Controller
{
public ActionResult MyAction(string details = "details", string myname = "")
{
...
and change your custom route to:
routes.MapRoute(
"myname", // Route name
"{controller}/{action}/{details}/{myname}", // URL with parameters
new { controller = "mycontroller", action = "myaction", details= UrlParameter.Optional, myname= UrlParameter.Optional } // Parameter defaults
);
But you can define only one controller and only one action, rest of the routeTemplate are parameters.
You can't define two action in one route. It make no sense.

MVC 3 How to use MapRoute

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 = "")
{
}

Categories