asp.net mvc outbound routing from a GET form post - asp.net

I have a route that looks like this:
new { controller = "ControllerName", action = "PhotoGallery", slug = "photo-gallery", filtertype = UrlParameter.Optional, filtervalue = UrlParameter.Optional, sku = UrlParameter.Optional}
and I have a form that looks like this:
<%using(Html.BeginForm("PhotoGallery", "ControllerName", FormMethod.Get)) {%>
<%:Html.Hidden("filtertype", "1")%>
<%:Html.DropDownList("filtervalue", ViewData["Designers"] as SelectList, "Photos by designer", new { onchange = "selectJump(this)" })%>
<%}%>
Right now when the form is submitted I get the form values appended to the url as query strings (?filtertype=1 etc) Is there a way to get this form to use routing to render the URL?
So the form would post a URL that looked like this:
www.site.com/photo-gallery/1/selectedvalue
and not like"
www.site.com/photo-gallery?filtertype=1&filtervalue=selectedvalue
Thanks!

If you only have some of the parameters present, Mvc will create a Query String type Url unless it finds an exact matching url.
Suggest you will need something like:
routes.Add("testRoute",
new Route("/{action}/{controller}/{slug}/{filtertype}/{filtervalue}",
new { controller = "ControllerName", action = "PhotoGallery", slug = "photo-gallery", filtertype = UrlParameter.Optional, filtervalue = UrlParameter.Optional}
);
make sure this is before your existing route

Related

How to display friendly url with Asp.net MVC instead of the controller and action names

My url is being displayed something like this: http:\mydomain.com.br\Cars\Search\23
the Cars is my controller, Search is my action and 23 is route parameter. I am not happy with this url mainly because of the uppercase letters. I would like to change it to http:\mydomain.com.br\result_car_search\23. I want to change the url without changing the structures of my controllers/actions.
I've using this to generate the "ugly" url.
#Html.ActionLink("Volkswagem Gol", "Search", "Car", new { param = 93}, null)
I've tried to use
#Html.RouteLink("Fiat Palio", "result_car_search", new { param = 30}, null)
but for my surprise it generage a url pointing to the current url with the route parameter appended to the end.
my route is this:
routes.MapRoute("result_car_search", "{controller}/{action}/{param}", new { controller = "Car", action = "Search", param= UrlParameter.Optional });

ASP.NET Multiple route and invalid dataroute values

I have multiple routes which refer to different controllers and a few of routes have identical number of parameter. please look at my sample below
routes.MapRoute("AdInfo", "{controller}/{action}/{AdGUID}/{UserID}/{Category}",
new
{
controller = "Home",
action = "DetailAd",
AdGUID = UrlParameter.Optional,
UserID = UrlParameter.Optional,
Category = UrlParameter.Optional
});
routes.MapRoute("PostAd", "{controller}/{action}/{MainCategory}/{SubCategory}/{SubCategoryGUID}",
new
{
controller = "Classified",
action = "Post",
MainCategory = UrlParameter.Optional,
SubCategory = UrlParameter.Optional,
SubCategoryGUID = UrlParameter.Optional
});
Routes AdInfo and PostAd have three parameters but both of them refer to different controller and action. asp.net mvc interpret wrongly, when i click URL which are suppose to link to Controller Classified - action post with route data values MainCategory, subcategory, and subcategoryGUID. somehow, the route data values are AdGUID,UserID,and category.
Do you have any idea how to solve this issue?
You're almost there, basically you're creating a catch all route and specifying the default, except you have two catchall routes setup. It should be like this at the very least:
routes.MapRoute("AdInfo", "Home/{action}/{AdGUID}/{UserID}/{Category}",
new
{
controller = "Home",
action = "DetailAd",
AdGUID = UrlParameter.Optional,
UserID = UrlParameter.Optional,
Category = UrlParameter.Optional
});
routes.MapRoute("PostAd", "Classified/{action}/{MainCategory}/{SubCategory}/{SubCategoryGUID}",
new
{
controller = "Classified",
action = "Post",
MainCategory = UrlParameter.Optional,
SubCategory = UrlParameter.Optional,
SubCategoryGUID = UrlParameter.Optional
});
Of course in some situations this won't work. The only way I can see being able to use this route for multiple controllers too is by adding a suffix after the controller like this:
routes.MapRoute("AdInfo", "{Controller}/{action}/SomeNameThatSeperatesThisRouteFromTheNext/{AdGUID}/{UserID}/{Category}",
Or if on the second route the action will always be the same, then just hard code it into the route and now you will have two distinct routes (Make sure the route you hard code is declared before the other route).

ASP.NET MVC 3 RC Html.Actionlink not generating link

As the title says...
I have a route (the first one listed) which looks like this:
routes.MapRoute(
"TopicRoute", // Route name
"forums/{forumSlug}/{topicSlug}", // URL with parameters
new { controller = "Forums", action = "Topic"} // Parameter defaults
);
I can browse to:
/forums/my-forum/my-topic
and the page loads fine. Yet I have a Html.ActionLink that looks like:
#Html.ActionLink(item.Title, "Topic", new { forumSlug ="my-forum", topicSlug = "my-topic" })
And it won't generate the correct link syntax for me? It generates:
My Topic
Don't forget the controller:
#Html.ActionLink(item.Title, "Topic",
new { forumSlug ="my-forum", topicSlug = "my-topic", controller = "Forums" })
or use a named route link:
#Html.RouteLink(item.Title, "TopicRoute",
new { forumSlug = "my-forum", topicSlug = "my-topic" })

ASP.NET MVC : Ignore latter route elements in url for a form's action

I have a URL /products/search where Products is the controller and Search is the action. This url contains a search form whose action attribute is (and should always be) /products/search eg;
<%using( Html.BeginForm( "search", "products", FormMethod.Post))
This works ok until I introduce paging in the search results. For example if I search for "t" I get a paged list. So on page 2 my url looks like this :
/products/search/t/2
It shows page 2 of the result set for the search "t". The problem is that the form action is now also /products/search/t/2. I want the form to always post to /products/search.
My routes are :
routes.MapRoute( "Products search",
"products/search/{query}/{page}",
new { controller = "Products", action = "Search", query = "", page = 1 });
routes.MapRoute( "Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" });
How can I force Html.BeginForm(), or more specifically Url.Action( "Search", "Products"), to ignore the /query/page in the url?
Thanks
Fixed by adding another route where query and page are not in the url which is kind of counter intuitive because the more specific route is lower down the order
routes.MapRoute(
"",
"products/search",
new { controller = "Products", action = "Search", query = "", page = 1 });
routes.MapRoute(
"",
"products/search/{query}/{page}",
new { controller = "Products", action = "Search"} //, query = "", page = 1 });
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" });
The order is really important, the first route it hits that matches it uses. Therefore catch all type routes should be at the very end. Here is a helpful route debugger youc an use to figure out what the problem is.
Route Debugger

ASP.NET - ActionResult parameter is coming back null always when passing string - why?

I am barely starting out with my first project on the ASP.NET MVC project type and I am creating a Details page where instead of passing the templated (int id), I would like to pass a string instead. But when I am in debug mode, and enter this in the URL, "myString" is null. Why so? Do I have to change anything else somehwere else?
So if I go to the URL and enter this:
http://localhost:2345/Bank/EmployeeDetails/3d34xyz
public ActionResult EmployeeDetails(string myString) // myString is null
{
return View();
}
In you Global.asax.cs file, you will have the following route mapped by default:
routes.mapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
That means that an url like http://localhost:2345/Bank/EmployeeDetails/3d34xyz will go to the Bank controller, the EmployeeDetails action and pass the value 3d34xyz into a parameter named id. It is perfectly alright to pass a string, but in order to make it work you have two options:
1) Rename the variable to id in your action method.
public ActionResult EmployeeDetails(string id) { ... }
2) Add another route that matches whatever name you want for your string. Make sure to make it more specific than the default route, and to place it before the default route in the Global.asax.cs file.
routes.mapRoute(
"BankEmployeeDetails"
"Bank/EmployeeDetails/{myString}"
new { controller = "Bank", action = "EmployeeDetails", myString = UrlParameter.Optional });
This will pass a default value of null to myString if no value is passed in the url, but with the url you specified you will pass the value 3d34xyz.
Rename myString to id if you are using the default route table.
Assuming you haven't modified the default routes (In your Global.asax.cs):
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
The method is expecting it to be named "id".
Change the name of myString to id.
I had the same problem, you just need to provide the id in the page where you the hyperlink for Details(in the Bank page here)
#Html.ActionLink("Details", "Details", new { id=""})
This solved my problem hope it helps you.
This for MVC 5.
And when you have tryed all of them and still it comes back null then give a full restart for Visual Studio. That's what eventually helped for me.

Resources