I am working with URL routing , and have some issues. I want my url to be like this:
www.domain.com/p/myproduct
But I also want to be able to retrieve the ID of the product, without accessing the database. I thought about having a URL like:
www.domain.com/p/myproduct/1
But if I could hide the ID it would be better.
So, how do I do it the simplest way?
Currently my Global.asax has the following route:
routes.MapLocalizedRoute("Product",
"p/{productId}/{SeName}",
new { controller = "Catalog", action = "Product", SeName = UrlParameter.Optional },
new { productId = #"\d+" },
new[] { "Nop.Web.Controllers" });
If all you have is the url then you will have to include the ID there if you want to read it.
The only way to hide it is if you have the ID coming to you from the post of a form say, assuming they have come from a previous page. Then you could store the selected Id and post to url as part of the request.
Related
Is it possible to add some route that always add some param in url?
For example when I write something link this
#Html.ActionLink("Site", "Index", "Admin")
It routes to admin/index?key=some_auth_key. For example I save this param in some static class/variable.
4th parameter of Html.ActionLink can have any number of properties:
Html.ActionLink("Check this", "Edit", "test",
new { id = id, data=name }, new { style = "display:block" })
These properties are inserted into URL based on routing, but if the property name cannot be matched into any route it is added as URL GET parameter.
So if you have standard route {controller}/{action}/{id}, you will get the URL:
test/Edit/[id]?data=[name]
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 });
As the title says.
I have a route set up and working fine, which provides a default action when none is specified. I just want to hide the action from the URL because it's unnecessary clutter.
Setting the "ActionName" parameter as null, or "", will just result in the current page's action being substituted instead - which doesn't work.
I'm open to using #Html.ActionLink() if that will get me what I need.
My route definition is
routes.MapRoute(
name: "MyBookRoute",
url: "Book/{id}",
defaults: new { controller = "Book", action = "Index" }
);
If all else fails, I suppose I can deal with writing out the hrefs manually, but this should not be a difficult thing for Razor to do.
Has anyone else come across this and knows what to do?
Base on your route definition, then either
#Url.Action("Index", "Book", new { id = 1 })
or
#Html.ActionLink("Your link text", "Index", "Book", new { id = 1 }, null)
will remove the action name from the generated url.
I want to set up a route in my ASP.NET MVC 3 web application, that has a url fragment appended to it, in the same way as some url's on StackOverflow, e.g:
http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732382#1732382
However, I can't see any (clean) way of achieving this at the moment.
Is there an 'out of the box' solution or do I need to build a helper of some description to append a url fragment to the end of a normal route?
You could use helpers. For example:
#Html.ActionLink(
"linkText",
"action",
"controller",
null,
null,
"fragment",
new { id = "123" },
new { #class = "test" }
)
or if you want only an url you could use the GenerateUrl method.
How to hide controller name in Url?
I use the ASP.NET MVC.
The original url is: http://www.sample.com/Users.mvc/UserDetail/9615
The "Users" is controller name, the "UserDetail" is action name, and the "9615" is UserId.
How can I hide the controller name and action name in the url.
Just like this: http://www.sample.com/9615
I have writed the following code in the Global.ascx.cs to hide the action name:
routes.MapRoute(
"UserDetail", // Route name
"Users.mvc/{UserId}", // URL with parameters
new { controller = "Users", action = "UserDetail", UserId = "" } // Parameter defaults
);
Using the above code I hid the action name and got this url: http://www.sample.com/Users.mvc/9615
But how can I hide the controller name and get this url: http://www.sample.com/9615
Thanks.
The idea is the same. You do just the thing you did to the action. However, your problem arises from the fact that IIS is probably not mapping www.xyz.com/1234 to ASP.NET runtime. To do so in IIS7, enable integrated mode and in IIS6, add a wildcard mapping in handler map that maps everything to ASP.NET.
To add a wildcard map, see http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx (Search for "IIS6 Extension-less URLs" in that page)
After that, simply add a route:
routes.MapRoute("UserDetails", "{UserID}/{*name}",
new { controller = "Users", action = "UserDetail" , UserID=""});
This should do the trick.
MVC recognizes the difference between "{UserID}" and "{id}" so if you are going to have a route with only "{UserID}" in the Url you need to place it first in the list other wise it never gets hit. And make sure the default includes "id" since it will continually loop over "UserDetails" unless the default references id as apposed to UserID. I found this format works for me:
routes.MapRoute("UserDetails",
"{UserID}",
new { controller = "Users", action = "UserDetail", id = "" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Account", action = "LogOn", id = "" } // Parameter defaults
);