Asp.net mvc Details method - asp.net

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.

Related

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

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

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

ASP.NET MVC 3 RC 2 Routing problem

I changed the default routing in ASP.NET MVC from
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
to
routes.MapRoute(
"Default",
"{controller}/{action}/{id}/{lineNo}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional, lineNo = UrlParameter.Optional });
but now all #Html.ActionLink() calls are rendered to href="". If I change the route back to default all links are working again.
I used the same route with RC1 and it worked perfectly.
I didn't find anything in the release docs so I think I'm doing it wrong.
Regards,
Steffen
In a route an optional parameter can appear only at the end. This means that in your route definition the id parameter cannot be optional. You need to explicitly set it to a value.
routes.MapRoute(
"Default",
"{controller}/{action}/{id}/{lineNo}",
new {
controller = "Home",
action = "Index",
lineNo = UrlParameter.Optional
}
);
And when you generate a link you must always provide a value for the id parameter if you want this route to match:
#Html.ActionLink("some link", "index", new { id = "123" })
As an alternative you might give a default value to the id parameter:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}/{lineNo}",
new {
controller = "Home",
action = "Index",
id = "123",
lineNo = UrlParameter.Optional
}
);
Now you no longer need to specify it in your links.

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