I am using asp.net mvc2 and would like my site to show details (of 123) if a user enters foo.com/123.
What is the route value I should specify for this, and in what order?
I tried
routes.MapRoute(
name: "foobar",
url: "{id}",
defaults: new { controller = "foo", action = "bar", id = UrlParameter.Optional }
);
but I get a 404.
Any help is appreciated.
routes.MapRoute( name: "foobar", url: "{id}", defaults: new { controller = "foo", action = "bar", id = UrlParameter.Optional } );
should be as follows
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "foo", action = "bar", id = ""// Parameter defaults
);
Related
I want to route request to specific version controller based on version number in URI.
for example,
routes.MapHttpRoute(
name: "APIV2",
routeTemplate: "BlogFeed/{version}/{id}",
defaults: new { id = RouteParameter.Optional, controller = "BlogFeedV2"}
);
What I want is that based on version route parameter I want to route request to that versioned controller. so if version route parameter is v2, that request should be handled by BlogFeedV2. Is there a way to handle this?
why not :
routes.MapHttpRoute(
name: "APIV2",
routeTemplate: "BlogFeed/v2/{id}",
defaults: new { id = RouteParameter.Optional, controller = "BlogFeedV2"}
);
routes.MapHttpRoute(
name: "APIV1",
routeTemplate: "BlogFeed/v1/{id}",
defaults: new { id = RouteParameter.Optional, controller = "BlogFeedV1"}
);
So, I have implemented custom routing in mysite.
routes.MapRoute(
"WithFriendlyNameOnly",
"{friendlyName}",
new { controller = "Home", action = "Redirect", friendlyName = String.Empty, id = UrlParameter.Optional },
new { friendlyName = new MustBeFriendlyName() }
);
routes.MapRoute(
"WithFriendlyNameDefault",
"{friendlyName}/{controller}",
new { controller = "Home", action = "Index", friendlyName = String.Empty, id = UrlParameter.Optional },
new { friendlyName = new MustBeFriendlyName() }
);
routes.MapRoute(
"WithFriendlyName",
"{friendlyName}/{controller}/{action}/{id}",
new { controller = "Home", action = "Redirect", friendlyName = String.Empty, id = UrlParameter.Optional },
new { friendlyName = new MustBeFriendlyName() }
);
routes.MapRoute(
"DefaultWithRule", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = new MustNotRequireFriendlyName() }
);
This works fine locally. I am using the friendly name to determine your context in site site. Without it, all I give you is a 404 page.
My test environment is a Windows 2008 Server (IIS 7.5), and my application is in a subfolder like this: test.mydomain.com/mysite. Everything worked fine until I did this new custom url, so I'm fairly sure it isn't IIS or the server. However, my custom routes are not working at all. My regular routes are working just fine - like the home page, some information pages etc. It is only the pages with my custom routing. What did I do wrong?
Have you tried to switch routes order making more specific at the beginning? It worked for me.
Try also that debugger: http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
Helps very much!
Been new to MVC 2, how can we get a link as:
http://localhost:13269/Terms
instead
http://localhost:13269/Frontend/Terms
as this is the result of:
<%: Html.ActionLink("Terms & Conditions", "Terms", "Frontpage")%>
even if I don't specify the Controller like <%: Html.ActionLink("Terms & Conditions", "Terms")%>
as I changed the route to
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Frontend", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Try this as a new route definition (declare this before the default route)
routes.MapRoute(
"DefaultFontEnd", // Route name
"{action}/{id}", // URL with parameters
new { controller = "Frontend", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I'm trying to use Html.ActionLink to generate a link with in this form:
/Action/Model/Id/Parameter1/Parameter2
I've used:
<%= Html.ActionLink("Link Text", "Action", "Model", new { id = var, parament1=var1 }, null) %>
but it always ends up looking like /Action/Model/Id?parameter1=variable
I've seen similar questions on Stackoverflow and elsewhere, but I can't find a solution that works/makes sense. Any help would be appreciated.
#John: I have the following route registered: routes.MapRoute("Alternate","{controller}/{action}/{id}/{Heading}", new { controller = "Designation", action = "Details", id = "", Heading = "" }
I then have the following code in my view: <%= Html.ActionLink("Link Text", "Action", "Model", new { Id = model.Id, Heading = model.Heading }, null) %>
I get /Model/Action/Id?Heading=var1 I want it as /Model/Action/Id/var1
ANSWER: I added the following route to my global.asax.cs file and the links generated correctly.
routes.MapRoute("Something", "MyModel/MyAction/{Id}/{Heading}", new { controller = "MyModel", action = "MyAction", id = "", heading = "" }, new { controller = #"[^.]*" });
I have the following default and only route:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
In my Site.Master I have the following:
<%= Html.ActionLink("Profile", "Details", "Users")%>
If I am on the following Url:
http://localhost:1155/Users/Details/1
and I click the link above it goes the same page.
Should it not go to the following url?
http://localhost:1155/Users/Details
For some reason it is keeping the id in the Url.
For some reason it is keeping the id in the Url.
It is by design.
Try this to get rid of id:
<%= Html.ActionLink("Profile", "Details", "Users", new { id = "" }, null)%>