Url routing - infinite url - asp.net-mvc-3

I use asp.net mvc3 and I want to write route that has no end..
I mean something like that:
www.site.com/Cameras/{id}={string}/{id}={string}/{id}={string}/{id}={string}.....
where the id represent a filter id and the string represent a value of that filter..
I have many types of filters and in the future I want to be able to add more without any dependence..
How should this kind of route should look? And how do I start to deal these parameters?

What you need to do is write a catchall route and then interpret it like this:
routes.MapRoute("Cameras", "cameras/{*url}",
new { controller = "Cameras", action = "Index" }
);
public ActionResult Index(string url)
{
var ids = url.split('/');
// now do what you need with the ids
}
You should use urls like this:
/cameras/id1/id2/id3/id4

Related

ASP.NET MVC Routing - Thousands of URL's

My ecommerce application stores URL's for item pages in the database. There are thousands of these URL's, which are all root level (i.e. domain-name.com/{item-page-url}).
If I add all of these URL's to the route table by using a simple for loop to call RouteCollection.MapRoute for each URL site performance degrades exponentially. Why? The reason for this is here.
How should I properly handle this situation? Adding all of the routes to the route table doesn't seem right (not to mention the performance pretty much confirms that). I've seen a few ideas about inspecting all incoming URL's and then trying to match that to the URL's in the database but don't fully understand how I'd implement that, nor am I sure if it's the best approach.
Any ideas or suggestions? This seems like it would be not so uncommon, but I haven't found a concrete way to handle it.
If you can change your route to
mycreativeshop.com/product/my-product-name then adding following route to the top of your route config file can help you.
routes.MapRoute(
"CustomRouteProduct",
"product/{id}",
new { controller = "yourcontrollername", action = "Index" }
);
and in the action map the parameter value with name of your product name
public ActionResult Index(string id)
{
//var prdName = id.Replace("-", " ");
//look up prdName in database
return View();
}
Update
Added following as a top route
routes.MapRoute(
"CustomRouteProductZX",
"{id}",
new { controller = "Content", action = "Index" }
);
and by accessing http://localhost:12025/Car-Paint I was directed to Index action of ContentController where I accessed "Car-Paint" in parameter id
But, above having above blocks patterns like http://localhost:12025/Home/ (here Home is also treated as a product)

Send space in route mvc

I have to implement search with MVC3, where user can search any word or words, now if i write only a sigle word its work fine and my route will be like
/search/toy
and toy is recognise by my controller method.
But if i want to search something with space like 'kid toy' then route have a space and in my controller method it doesn't recognise it as a word and throw error like
/search/kids%20toy
Has anyone implement such thing in his project plz help.
Thanks in advance.
Looks like you have an action called "Toy".
// maps to /search/
public class Search : Controller
{
// maps to /search/toy
public ActionResult Toy()
{
}
}
If you haven't modified the default routes at all, you need to use {controller}/{action}/{id}.
(Of course you'll have to look into routing if you want to do different kind of routes. Ask google for ASP.NET MVC routing)
Here's the quickest way:
// maps to /search
public class Search : Controller
{
// maps to /search/for/{id}
public ActionResult For(string id)
{
// search for id
}
}
Which could be used as /search/for/kids%20toy.
Or you can use querystring for parameters named something else than id.
// maps to /search
public class Search : Controller
{
// maps to /search/ by default, check route config
public ActionResult Index(string query)
{
// search for query
}
}
To be able to use {controller}/{action}/{query} or {controller}/{query} you'll have to create a route, but you can address that one by
/search/?query=kids%20toy

How can I specify the name of the controller in ASP.NET Web API?

I have an ApiController, quite simple, like this:
public class AssetController : ApiController
{
// removed for brevity
}
When I insert a route to it from a view, the url created is something like:
http://host/Asset
but I would like to customize the name, so that it becomes this:
http://host/assets
How can I specify a custom name for my controller, without resorting to a complete custom routing table?
When I insert a route to it from a view, the url created is something like: http://host/Asset
You haven't really shown how you are doing this inserting but the following should work fine:
#Url.RouteUrl("DefaultApi", new { httproute = "false", controller = "assets" })
and if you want an absolute url you could specify the protocol scheme as third argument:
#Url.RouteUrl("DefaultApi", new { httproute = "false", controller = "assets" }, "http")
And in order to obey RESTFul conventions you should rename your controller to AssetsController.
I'd recommend looking at the https://github.com/mccalltd/AttributeRouting library. It handles this aspect quite well by putting an attribute right on each function and giving it a specific route (which can be anything).
I've had to resolve this issue so I've opted to adjust my routing table to reflect the API that I really want.

route that will handle URL's in the URL for MVC 3

I am trying to create a custom route that can handle something like:
domain.com/link/http://www.someotherdomain.com/blablah.html?qstring=54
Where the passed param is a link...
I cannot get this to work with URL encode and decode.. always returns a bad request?
Pass the link as id.
Something like...
controller:
public SomeAction(string url)
{
...
}
View:
#Html.ActionLink("link name", "Action", new {id = "someurl.com"}
or modify the global.asax's routes.MapRoute and add another parameter.

Action parameters showing up in querystring instead of URL

I'm doing this:
#Url.Action("Details", "MyController", new { id = this.Model.ID })
The URLcomes out like this: /MyController/Details?id=1
How do I get it to format the URL like this: /MyController/Details/1
The routes look like this:
routes.MapRoute("Default", "{Controller}/{Action}", new { Controller = "Home", Action = "Index" });
routes.MapRoute("Default-ID", "{Controller}/{Action}/{ID}");
The order of routes matters - both urls are valid, and in this case the system gets to the query string one first when looking for a url matching that action.
There's also a possibility you have a case sensitivity issue with {ID} - not sure about that one, but generally it is best to use case consistently.

Resources