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 = "")
{
}
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'm trying to add custom URL to my application. For example, I have a report section and I want all the Controller in that section to start with "Report". Currently, my URL look like :
localhost:12345/MyReport
but I want it to look like
localhost:12345/Report/MyReport
MyReport is the name of my controller.
I tried to change the following code in Global.asax
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
By
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Report",
"Report/{controller}/{action}/{id}",
new { controller = "MyReport", action = "Index", id = UrlParameter.Optional }
);
But I get a page not found error when I try to access localhost:12345/Report/MyReport... Any clue?
As crazy as this is at first, order matters. Routing will find the first route that matches, and serves it. Try putting the the default one after your new custom one.
Because of that, you're going to the controller of Report, and attempting to find the action of MyReport. Which is indeed not found.
I have a web site with something like this:
http://website/Controller/Action/Id
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Controller", action = "Action", id = UrlParameter.Optional } // Parameter defaults
);
Is there any way to route to the same action if the url changes to be
http://website/Action/Id
I was trying to change global.asax but nothing is working.
Based on how I'm reading your question, you're saying that you may only ever have one controller and want to be able to route all actions to that single controller?
If I'm reading it correctly, you should be able to do something similar to this:
routes.MapRoute(
"Default", // Route name
"{action}/{id}", // URL with parameters
new { controller = "Controller", action = "Action", id = UrlParameter.Optional } // Parameter defaults
);
//standard routing
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
//the custom route I added
routes.Add(
new Route("Fish/Image",
new ImageHandlerRouteHandler()
)
);
I thought this would use my ImageHandlerRouteHandler but all I get at Fish/Image is a 404.
Add the route before the default route.
routes.Add(
new Route("Fish/Image",
new ImageHandlerRouteHandler()
)
);
//standard routing
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The request is mapped to the first matching route in routes table. Since default route have no restrictions, it gets called before your custom route.
When you have the Fish/Image route before the default route, the problem you will have in MVC building links is that the Fish/Image route appears to satisfy ALL requests. In order to have this route work, change it as follows:
routes.Add(
"FishImage",
new Route(
"{controller}/Image",
new RouteValueDictionary(new { controller = "Fish" }),
new RouteValueDictionary(new { controller = #"^(?!fish).+" }),
null,
new ImageHandlerRouteHandler()
)
);
This changes the route, so that on the link building side, your other links will fail this route. Currently, MVC sees that it can build any link to match this route, so it does. With the constrained route, MVC will now see that other links will fail this route.
The order that routes appear are important. Add your route first and left the Default as a fall back.
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.