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)%>
Related
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
);
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
);
in my Asp.net MVC2 app I have registered the following routes in the global.asax.cs:
routes.MapRoute(
"Search",
"Search/{action}/{category}/{query}/{page}",
new { controller = "Search", action = "Results", category = "All", page = 1 },
new { page = #"\d{1,8}" }
);
// URL: /Search
routes.MapRoute(
"SearchDefault",
"Search",
new { controller = "Search", action="Index" }
);
routes.MapRoute(
"Product",
"Product/{action}/{productcode}",
new { controller = "Product", action = "Details" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Search", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I have my SearchController:
public ActionResult Results(string category, string query, int page)
{
}
I have my ProductController:
public ActionResult Details(string productcode)
{
return View();
}
In my Results.aspx View, the following ActionLinks exist:
<% foreach (var cat in Model.Categories) { %>
<li><%= Html.ActionLink(cat.Name, "Results", "Search", new { category= cat.Name, query = Model.SearchText, page=1 }, null)%></li>
<% } %>
</ul>
<hr />
<table>
<% foreach (var p in Model.Products) { %>
<tr>
<td>
<%= Html.ActionLink(p.ProductName, "Details", "Product", new { product = p.ProductCode }, new { })%><br />
</td>
</tr>
<% } %>
The first actionlink is rendering as:
"http://localhost/Search/Results?category=Test%20Category%20A&query=test%20product&page=1"
whereas the second ActionLink is correctly rendering:
"http://localhost/Product/Details/1234ABC020848"
The strange thing is that both work correctly and even if I manually type in:
"http://localhost/Search/Results/Test%20Category%20A/test%20product/1"
then my SearchController correctly executes also. I really would rather have the cleaner URL rendered by my ActionLink. What have I missed?
Thanks in advance.
As I donĀ“t have your model, I did remove the foreach loops and I replace all unknown values by strings. In my tests I found the opposite behavior: the first link was ok while the other was not cleaner. The fix for the second action link was to replace "product" by "productcode".
<ul>
<li><%= Html.ActionLink("Category", "Results", "Search", new { category= "Test Category A", query = "test product", page=2 }, null)%></li>
</ul>
<hr />
<table>
<tr>
<td>
<%= Html.ActionLink("Product", "Details", "Product", new { productcode = "1234ABC020848" }, new { })%><br />
</td>
</tr>
</table>
Both ways are suppose to work as the routing system is responsible for mapping the variables.
routes.MapRoute(
"SearchDefault",
"{controller}/{action}/{category}/{query}/{page}",
new { },
new { controller = "Search", action = "Results" }
);
This should get you url like this (some additional tweaks may be required)
http://localhost/Search/Results/Test%20Category%20A/test%20product/1
Ignore that... I didn't see the first route in the question.
I usually like to use query string for searches tho, because once you got two or more parameters that aren't required, routes for that are kinda pain to build and maintain.
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 = #"[^.]*" });