RedirectToAction As Get Request format - c#

I have an action result that I want to redirect to an action with a dynamic ID.
return RedirectToAction("Engine", new {id = latestVersion.Id});
However, the URL that returns ends up being:
domain.com/project/engine/xxx
what I need however is it to be:
domain.com/project/engine?id=xxx
Here are my current maproutes:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"PrevSession", // Route name
"{controller}/{action}/{id}/{id2}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional, id2 = UrlParameter.Optional } // Parameter defaults
);
Is there a way to change the way this is formatted at the controller level?

Option 1.
you can change the id route param to some other name like,
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{myId}", // URL with parameters
new { controller = "Home", action = "Index", myId = UrlParameter.Optional } // Parameter defaults
);
and change accordingly in the controllers(where they are used), or
Option 2.
You can add a dummy route definition without id parameter like,
routes.MapRoute(
"RouteWithoutId", // Route name
"{controller}/{action}"
);
and use RedirectToRoute method like,
return RedirectToRoute("RouteWithoutId", new { action = "Engine", id = latestVersion.Id});
hope this helps.

Simple answer
routes.MapRoute(
"Default", // Route name
"{controller}/{action}", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);

Related

c# Route config for specific URL only

I have this URL
http://localhost:51095/Person/Walk/10
Using RouteConfig how can I remove the controller part without affecting other URL's.
So this will become
http://localhost:51095/Walk/10
This is the order also, they are affecting other URL's
routes.MapRoute(
"Walk", // Route name
"{action}/{distance}", // URL with parameters
new { controller = "Person", action = "Walk" } // Parameter defaults,
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
But this one also affected other Routes
You'll have to be more specific:
routes.MapRoute("Walk",
"Walk/{distance}",
new { controller = "Person", action = "Walk" });

RegisterRoutes in Global.asax

I dont understand the RegisterRoutes perfectly. Lets assume the browser's current URL is
//Home/ListCompanies/{filter}
http://localhost:21047/Home/ListProducers/Yerli
we came to the address above with the link below
#Html.ActionLink("Yerli Markalar", "ListProducers/Yerli", "Home")
And I have a link on the current page like below
#Html.ActionLink("Bayiler", "ListCompanies", "Home")
http://localhost:21047/Home/ListCompanies/Yerli
But when I hover on the last link, it displays "Yerli" filter as well which I dont give it in the link
Why does the filter "Yerli" come here? It should seem like below
http://localhost:21047/Home/ListCompanies/
What am I doing wrong?
here are my Routes
routes.MapRoute(
null,
"Home/ListProducers/{filter}", // Route name
new { controller = "Home", action = "ListProducers", filter = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
null,
"Home/ListCompanies/{filter}", // Route name
new { controller = "Home", action = "ListCompanies", filter = UrlParameter.Optional } // Parameter defaults
);
Firstly, you do not use
#Html.ActionLink("Yerli Markalar", "ListProducers/Yerli", "Home")
If "Yerli" is the parameter to be passed to
public ActionResult ListProducers(string filter)
then it needs to be
#Html.ActionLink("Yerli Markalar", "ListProducers", "Home", new { filter = "Yerli" }, null)
which will generate ..../Home/ListCompanies/Yerli
Your problem is that you have generated the action parameter as ListCompanies/Yerli whereas it should be ListCompanies
Note you should also name your routes
routes.MapRoute(
"ListProducers", // name it!
"Home/ListProducers/{filter}", // Route name
new { controller = "Home", action = "ListProducers", filter = UrlParameter.Optional } // Parameter defaults
);
However, those routes seem unnecessary and you could just delete them and let the default route handle it
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
and just change the method to
public ActionResult ListProducers(string id)
and use
#Html.ActionLink("Yerli Markalar", "ListProducers", "Home", new { id = "Yerli" }, null)
which will produce exactly the same result

Set default action (instead of index) for controller in ASP.NET MVC 3

I have a controller called Dashboard with 3 actions: Summary, Details, and Status, none of which take an ID or any other parameters. I want the URL /Dashboard to route to the Summary action of the Dashboard controller, as /Dashboard/Summary does, but I can't figure out the correct way to add the route. In Global.asax.cs, I have the following:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new {controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
);
routes.MapRoute(
"/Dashboard",
"Dashboard",
new { controller = "Dashboard", action = "Summary" }
);
For the second part, I've also tried:
routes.MapRoute(
"/Dashboard",
"{controller}",
new { controller = "Dashboard", action = "Summary" }
);
and
routes.MapRoute(
"/Dashboard",
"{controller}",
new { action = "Summary" }
);
but I always get a 404 when trying to access /Dashboard. I'm pretty sure I'm missing something about the format for the parameters to MapRoute, but I don't know what it is...
Move your Dashboard route in front of the Default route:
routes.MapRoute(
"Dashboard",
"Dashboard/{action}",
new { controller = "Dashboard", action = "Summary" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new {controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
);
The order of routes changes everything. Also, notice the changes I made to the Dashboard route. The first parameter is the name of the route. Second is the URL, which match URLs that start with Dashboard, and allows for other actions in your Dashboard controller. As you can see, it will default to the Summary action.
You need to declare the "Default" catch-all route last.
This set default Action for any Controller asp.net:
routes.MapRoute("Dashboard", "{controller}/{action}",
defaults: new { controller = "Dashboard", action = "Summary" });

Changing Route Values in global.asax

I would like to change my default route values.
Right now, I have:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I would like to have: {controller}/{id}/{action}
It is not as simple as changing the value as I have already tried this. How do I approach this?
You will need 2 routes to accomplish this.
Just the rough idea, untested:
routes.MapRoute(
"Default", // Route name
"{controller}/{id}/{action}", // URL with parameters
new { controller = "Home", action = "Index", id=#"\d+" } // defaults
);
routes.MapRoute(
"Shorter", // Route name
"{controller}/{action}", // URL with parameters
new { controller = "Home", action = "Index" } // defaults
);
And do keep them in this order.
Not totally sure about the signature of the Actions, probably ActionResult Index(int? id)

Passing a parameter to an Index view

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.

Categories