How to change Ajax.ActionLink GET/POST url format [duplicate] - asp.net

Hi How do I map the url ../Companies/Results/value/id when both the parameters are optional?
Companies is the controller, Results is the action, value and id are optional parameters. On my form is a textbox for value and a selectlist for an id. The user could select both or one of each to search by. Tried something like this but cant handle when one of the optional parameters, say value, is missing such as ../Companies/Results/ /id
routes.MapRoute(
"Company+Profession", // Route name
"{action}/{value}/{profId}", // URL with parameters
new { controller = "Companies", action = "Index", value = UrlParameter.Optional, profId = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

You can't have a route that has two optional parameters, only the last parameter can be optional precisely because of the problem you describe. I suggest that you have a default parameter for value, like byid and use this when the person selects a profession.
I assume that you're constructing the URL via javascript, since using a GET form action would result in the parameter names being added to the URL. In this case, when the textbox is empty simply insert the default byid.
Update your route to include the default so any URLs that you generate will work. See Phil Haack's blog post on this for an alternative way to handle generating URLs that have two "optional" parameters.
// used when both parameters are specified
routes.MapRoute(
"Company+Profession", // Route name
"{action}/{value}/{profId}", // URL with parameters
new { controller = "Companies", action = "Index", value ="byid", profId = UrlParameter.Optional } // Parameter defaults
);

Thanks guys, just discovered route constraints for integer. And so fiddling around with some combinations of routes it seems to work the way I want :
routes.MapRoute(
"Detail", // Route name
"{action}/{value}", // URL with parameters
new { controller = "Companies", action = "Detail" }, // Parameter defaults
new { value = #"\d+" } //integer only
);
routes.MapRoute(
"Company + Profession", // Route name
"{action}/{value}/{profId}", // URL with parameters
new { controller = "Companies", action = "Results" }, // Parameter defaults
new { profId = #"\d+" } //integer only
);
routes.MapRoute(
"Profession", // Route name
"{action}/{profId}", // URL with parameters
new { controller = "Companies", action = "Results"}, // Parameter defaults
new {profId = #"\d+" } //integer only
);
routes.MapRoute(
"Company", // Route name
"{action}/{value}", // URL with parameters
new { controller = "Companies", action = "Results" } // Parameter defaults
);
routes.MapRoute(
"RootFolder", // Route name
"{action}/{value}", // URL with parameters
new { controller = "Companies", action = "Index", value = UrlParameter.Optional } // Parameter defaults
);

I'm not sure, since I don't have now where to try this, but here is my suggestion
routes.MapRoute(
"Company+Profession", // Route name
"Companies/{action}/value-{value}/id-{profId}", // URL with parameters
new { controller = "Companies", action = "Index", value = UrlParameter.Optional, profId = UrlParameter.Optional } // Parameter defaults
);

Related

In asp.net MVC if the url has a parameter , does the route have to be registered?

In my limited (2 weeks) experience in asp.net MVC3, for most action methods, I have never needed to add a route registration. But I have noticed that if the action method has an input parameter, then I can't access the method with a url of the form www.mysite.com/myController/myAction/myParameter1/myParameter2/myParameter3 (without the ? mark ) unless I map the route. Is that how its supposed to be?
By default, you already have registered route:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
it accepts one parameter, named id, so your action:
public ActionResult MyAction(string id)
will "catch" the request:
www.mysite.com/MyController/MyAction/parameter_value
and id will get value "parameter_value".
If you need more than one parameter (or parameter has to be names something else than "id"), then you have to register new route.
In case when you have 2 parameters, you will register route like this:
routes.MapRoute(
"Default",
"{controller}/{action}/{parameter1}/{parameter2}",
new { controller = "Home", action = "Index", parameter1 = UrlParameter.Optional, parameter2=UrlParameter.Optional }
);
and your action might be:
public ActionResult MyAction(string parameter1, int? parameter2)
Yeah, you need to register the route customizing the route in global.asax according to your requirement.You have to register the route in following way:
routes.MapRoute(
"routeName", // Route name
"{controller}/{action}/{myParameter}", // URL with parameters
new { controller = "Home", action = "Index", myParameter= "" } // Parameter defaults
);
So with above route, it ensures that whenever your url goes in above format, the parameter right after "action/" will be taken as parameter.....
For more than one parameter in your url, you can register like this:
routes.MapRoute(
"routeName", // Route name
"{controller}/{action}/{myParameter1}/{myParameter2}/{myParameter3}", // URL with parameters
new { controller = "Home", action = "Index", myParameter1= "", myParameter2= "", myParameter3= "" } // Parameter defaults
);

Url.Action does not use desired route for output

I have the following route definitions in my MVC3 website:
routes.MapRoute(
"FB", // Route name
"fb/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
).RouteHandler = new RH();
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
My custom "RH" handler's code is
public class RH : MvcRouteHandler
{
protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
{
//here I store somewhere that 'fb' prefix is used, so logic is different in some places
return base.GetHttpHandler(requestContext);
}
}
What I want to achieve, that when my website is accessed with the 'fb' subpath-prefix, then my website-logic executes a little bit different way.
The problem is, that when I access my site normally (eg. http://localhost), then when I execute
Url.Action('action' 'controller')
, then the output is "http://localhost/fb/controller/action".
I want to achieve, that when my site was accessed with 'fb' prefixed subpath, then my Url.Action calls output /fb/controller/action path, and if I access the website normally (without 'fb' prefix subpath), then Url.Action calls output /controller/action
The main thing, that /fb/controller/actions have to route to the same controllers/actions as when the site is accessed via /controller/action format.
The 'fb' route is just needed to store some temporary info when 'fb' prefix i used.
Seems I found a solution based on this link (MVC 3 Routing and Action Links not following expected contextual Route), new path-placeholder introduced and constraint added.
Maybe it's not good enough, or you know better than this, but seems to work for me:
routes.MapRoute(
"FB", // Route name
"{path}/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new { path = "fb" }
).RouteHandler = new RH();
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

Asp.net mvc Details method

I am working on a project, where I have used the default routing
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}/{name}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional, name = UrlParameter.Optional } // Parameter defaults
);
I have a users controller and methods like Index, create and details
I dont want to create links like http://mysite/users/details/111/somename
instead i want to create http://mysite/users/111/somename
like stackoverflow kind of url.
I know I can achieve by registering a route like this (before default route)
routes.MapRoute(
"UsersDetails", // Route name
"Users/{id}/{name}", // URL with parameters
new { controller = "Users", action = "Details", id = UrlParameter.Optional, name = UrlParameter.Optional } // Parameter defaults
);
but then all my other urls start creating problem. for example if i have url
http://somesite/users/registersuccess
Is there in workaround for this issue, or I should register all the urls.
Regards
Parminder
You'd need a constraint on your id parameter for your routes:
routes.MapRoute(
"UsersDetails",
"Users/{id}/{name}",
new { controller = "Users", action = "Details", id = UrlParameter.Optional, name = UrlParameter.Optional },
new { id="\d+" }
);
Then your other routes wouldn't be affected.

asp mvc routing with two optional parameters

Hi How do I map the url ../Companies/Results/value/id when both the parameters are optional?
Companies is the controller, Results is the action, value and id are optional parameters. On my form is a textbox for value and a selectlist for an id. The user could select both or one of each to search by. Tried something like this but cant handle when one of the optional parameters, say value, is missing such as ../Companies/Results/ /id
routes.MapRoute(
"Company+Profession", // Route name
"{action}/{value}/{profId}", // URL with parameters
new { controller = "Companies", action = "Index", value = UrlParameter.Optional, profId = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
You can't have a route that has two optional parameters, only the last parameter can be optional precisely because of the problem you describe. I suggest that you have a default parameter for value, like byid and use this when the person selects a profession.
I assume that you're constructing the URL via javascript, since using a GET form action would result in the parameter names being added to the URL. In this case, when the textbox is empty simply insert the default byid.
Update your route to include the default so any URLs that you generate will work. See Phil Haack's blog post on this for an alternative way to handle generating URLs that have two "optional" parameters.
// used when both parameters are specified
routes.MapRoute(
"Company+Profession", // Route name
"{action}/{value}/{profId}", // URL with parameters
new { controller = "Companies", action = "Index", value ="byid", profId = UrlParameter.Optional } // Parameter defaults
);
Thanks guys, just discovered route constraints for integer. And so fiddling around with some combinations of routes it seems to work the way I want :
routes.MapRoute(
"Detail", // Route name
"{action}/{value}", // URL with parameters
new { controller = "Companies", action = "Detail" }, // Parameter defaults
new { value = #"\d+" } //integer only
);
routes.MapRoute(
"Company + Profession", // Route name
"{action}/{value}/{profId}", // URL with parameters
new { controller = "Companies", action = "Results" }, // Parameter defaults
new { profId = #"\d+" } //integer only
);
routes.MapRoute(
"Profession", // Route name
"{action}/{profId}", // URL with parameters
new { controller = "Companies", action = "Results"}, // Parameter defaults
new {profId = #"\d+" } //integer only
);
routes.MapRoute(
"Company", // Route name
"{action}/{value}", // URL with parameters
new { controller = "Companies", action = "Results" } // Parameter defaults
);
routes.MapRoute(
"RootFolder", // Route name
"{action}/{value}", // URL with parameters
new { controller = "Companies", action = "Index", value = UrlParameter.Optional } // Parameter defaults
);
I'm not sure, since I don't have now where to try this, but here is my suggestion
routes.MapRoute(
"Company+Profession", // Route name
"Companies/{action}/value-{value}/id-{profId}", // URL with parameters
new { controller = "Companies", action = "Index", value = UrlParameter.Optional, profId = UrlParameter.Optional } // Parameter defaults
);

Routing table is not working

HI I have the following routing table
routes.MapRoute(null,
"Save", // Route name
new { controller = "Package", action = "Save"} // Parameter defaults
);
routes.MapRoute(
"Package", // Route name
"{controller}/{action}/{name}/{p}", // URL with parameters
new { controller = "Package", action = "Index", name = UrlParameter.Optional, p = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
When i am typing the path /Package/Save it is showing me page not found.
Can anybody please tell me what i am doing wrong?
THanks
The first route is configured incorrectly - name of route, then the pattern and then the defaults.
Also make sure you have a Package controller and a Save action method on it.

Resources