Content-Aware Route Mapping in ASP.NET MVC 2 - asp.net-mvc-2

I am working with ASP.NET MVC 2 and would like to optimize my routing. The desired result is as follows:
http://www.url.com/Sites/
http://www.url.com/Sites/Search/NY001
http://www.url.com/Sites/NY001
http://www.url.com/Sites/NY001/Photos
The problem with this scenario is that I want to treat the URL bits differently depending on their content, in a sense. As one can imagine, "NY001" is the identifier for a specific site, not for an action that lives in the Sites controller. My current solution is to define a special route map for the site-specific URLs and to specify each action in the Sites controller separately before that.
// currently in place, works
routes.MapRoute("SiteList", "Sites", new { controller = "Sites", action = "Index" });
routes.MapRoute("SiteSearch", "Sites/Search/{searchString}", new { controller = "Sites", action = "Index", searchString = "" });
routes.MapRoute("SiteNotFound", "Sites/NotFound", new { controller = "Sites", action = "NotFound" });
// and so on...
routes.MapRoute("Site", "Sites/{siteId}/{action}", new { controller = "Sites", action = "Index", siteId = "" });
As this is one of the central components of my web application, these actions are growing daily and the list is getting a bit unwieldy. I would love to be able to distill the long list of action-specific routes to a single declaration, similar to this:
routes.MapRoute("Sites", "Sites/{action}/{id}", new { controller = "Sites", action = "Index", id = UrlParameter.Optional });
The problem is that the routing framework has no way of differentiating between this new route and the site-specific one in the above example; therefore, both are matches. Am I stuck with declaring each action separately? Should I consider using a regular expression to differentiate based on the URLs' content?
Thanks in advance and I apologize in advance if this question has already been asked in a different form.

Not sure what exactly you want to accomplish. Of course you can't really map URLs with different signatures with one route, but you can reduce the list like this:
routes.MapRoute("Site", "Sites/{siteId}/{action}", new { controller = "Sites", action = "Index" }, new { action = "(?:Index|Photos|News)", siteId = #"[A-Z0-9]+" });
for urls:
http://www.url.com/Sites/NY001
http://www.url.com/Sites/NY001/Photos
http://www.url.com/Sites/NY001/News
This route will only map to actions Index, Photos, News (and whatever else you put in that reg. expression).

Related

Make ember-data use the URL that ember is loading for the REST request

On the system I am working on right now, I have had to try to tame ember-data a bit about how it does its REST request. The way that ember-data by default figures the URL for a certain request for a model is just not gonna cut it with the backend I am using.
What I need is, to get ember-data to use the same URL that ember is loading, but with a '?json' suffix. That is, if ember switches page to my band page, and the url is /bands, I want ember-data to request /bands?json for the data it needs, not whatever it figures from the name of the model. One could say, that I wanted the URL to be calculated from the path of the loading route, instead of from the name of the model being used.
I have tried by subclassing DS.RESTAdapter{} and see if I could get the buildURL method to do this, but I can't figure out how to get the URL ember is gonna load. The buildURL method is called before ember changes the location, so I can't use document.location.href or something. I can imagine I will need a way to ask ember what it is now loading, and what the URL is.
Any ideas of how to do this?
UPDATE
There hasn't been any satisfying solutions, so I decided to just do it the dirty way. This is it:
App.RouterSignature = [
['index', '/', '/index_models'],
['bands', '/bands', '/band_models'],
['band', '/band/:band_slug', '/band_model']
];
App.Router.map(function() {
for (var i = 0; i < App.RouterSignature.length; i++) {
var route = App.RouterSignature[i];
this.resource(route[0], {path: route[1]});
}
});
App.CustomAdapter = DS.RESTAdapter.extend({
buildURL: function(record, suffix) {
var url,
suffix = '?json',
needle = this._super(record);
for (var i = 0; i < App.RouterSignature.length && !url; i++) {
var route = App.RouterSignature[i];
if (route[2] == needle)
url = route[1];
}
return url + suffix;
}
});
Now App.Routes and DS.RESTAdapter.buildURL are based off the same data. The first two values in the App.RouterSignature list is just the name of the route, the path of the route. The third value is what DS.RESTAdapter.buildURL by default guesses should be the url. My custom adapter then takes that guess, matches it with one of the items in the App.RouterSignature list and then takes the second value from that item - the routes path.
Now the requests that ember-data makes is to the same url as the routes path.
You can try to setup your Adapter like so:
App.Adapter = DS.RESTAdapter.extend({
...
buildURL: function(record, suffix){
return this._super(record, suffix) + "?json";
}
});
App.Store = DS.Store.extend({
...
adapter: App.Adapter.create();
...
});
See here for more info on the RESTAdapter buildURL method.
Hope it helps

MVC routing not working when default and custom route refer to same controller

I have two routers in my global.asax, one is a default router which is like:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Test", action = "Action1", id = UrlParameter.Optional }
);
and other is Custom router:
routes.MapRoute(
"CustomRouter", // Route name
"Test/{id}/{FacetString}/{CurrPageNo}",
new { controller = "Test", action = "Action12", id = "", FacetString = UrlParameter.Optional, CurrPageNo=UrlParameter.Optional }
);
some how when I pass url "http://localhost/Test/1001/State=TX" the second router should get executed but some how its not executing.
I had read that the sequence of router is important, hence I tried to exchange there sequence, but it is still not working, if I place it above default router than, the cutom router gets called for all the other actions in that router, which should not happen
the last URL-component State=TX looks like a query string parameter to me. Shouldn't it be ?State=TX (which then wouldn't match your route) or /State/TX
it seems like you should use constraints, reducing match rate for your custom router. You can use forth parameter to define your constraints. In this case it can be something like this
routes.MapRoute(
"CustomRouter", // Route name
"Test/{id}/{FacetString}/{CurrPageNo}",
new { controller = "Test", action = "Action12", id = "", FacetString = UrlParameter.Optional, CurrPageNo=UrlParameter.Optional
, new {id=#"\d+"});
in this way your second URL section requires to be numeric in order to get executed.
According your second route your url should be in one of these formats
http://localhost/Test/1001
http://localhost/Test/1001/State
http://localhost/Test/1001/State/3
Also there is no need of controller = "Test", action = "Action12" as they are not part of the second route definition
Have a look at this MSDN link on ASP.NET routing

Asp.net Mvc 2 How to reroute to www.example.com/my-profile

i would like to reroute this address www.exmaple.com/my-profile to a profile controller i have in my mvc application. There is my global.aspx, as you can the the default routing is
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}/", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
This is the Maproute i'm trying to use to reroute to www.exmaple.com/my-profile
routes.MapRoute(
"Profile", // Route name
"{profile_name}/", // URL with parameters
new { controller = "Portfolio", action = "Index", page = UrlParameter.Optional } // Parameter defaults
);
The problem is when i type www.exmaple.com/profile it trys to direct me to the default maproute, not the one i have specified.
But when i can do this www.example.com/profile/my-profile
routes.MapRouteLowercase(
"Profile", // Route name
"profile/{profile_name}/", // URL with parameters
new { controller = "Portfolio", action = "Index", page = UrlParameter.Optional } // Parameter defaults
);
it works fine, but i dont want to add profile before my-profile. I would like it to work like facebook.com/my-profile or youtube.com/my-profile.
Does anyone one know how i accomplish this, i have been looking for a solution for over 2 months (on and off)
Thanks
Your routing if I remember rightly will return the first matching pattern for the URL. I would expect in your case, your URL is matching your default pattern first.
Move your more specific route above your 'default' route and see if this helps.

MVC 2 multilanguage in URL

I'm building a multilanguage application with MVC 2. I read a lot of posts about different ways to get the user's language. I think the best way to save the language, is to put it into the URL like this: www.example.de/language/controller/view.
My questions:
1. how should global.asax.cs look? I tried it to setting something below but it did not work:
routes.MapRoute("Default with language", "{lang}/{controller}/{action}/{id}", new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional,
}, new { lang = "de|en" });
routes.MapRoute("Default", "{controller}/{action}/{id}", new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional,
lang = "en",
});
There was a "en" for example in the route, but it always did not take the Resourse.en, it only took the default.
Question: How to get the current language in the controller?
Question: How to change the language?
Maybe this can help?
MVC Localization route

How to get the complete request that calls my MVC2 controller?

Newbie question … sorry ;-)
I have to write and to integrate a new website in a complex web application.
My new (MVC2) website will be hosted on a separate server and only called when the user clicks on a link in the already existing, complex website.
Means I(!) define the URL which calls my(!) new website.
But “they” (the calling, already existing, complex web application/website) will add an attribute to the url. This attribute is the sessionID.
Ok, I think I understand already that this calls my (MVC2) controller.
But how can I get in my (MVC2) controller the “calling URL” (which include the added sessionID)?
Hopefully that someone understand what I ask ;-)
Thanks in advance!
I want just share my little parser - hopefully it helps someone. ;-)
Also requests like
(Request.Url.Query =) "?sessionID=12345678901234567890123456789012&argumentWithoutValue&x=1&y&z=3"
will be well parsed.
Here my code:
Hashtable attributes = new Hashtable();
string query = Request.Url.Query;
string[] arrPairs = query.Split('&'); // ...?x=1&y=2
if (arrPairs != null)
{
foreach(string s in arrPairs)
{
if (!String.IsNullOrEmpty(s))
{
string onePair = s.Replace("?", "").Replace("&", "");
if (onePair.Contains("="))
{
string[] arr = onePair.Split('=');
if (arr != null)
{
if (arr.Count() == 2)
{
attributes.Add(arr[0], arr[1]);
}
}
}
else
{
// onePair does not contain a pair!
attributes.Add(onePair, "");
}
}
}
You really should set your URL and Route to be more MVC-Like. The URL you are calling should be:
newapp/controller/action/sessionId
Then set your route up:
routes.MapRoute(
"sessionId",
"{controller}/{action}/{sessionId}",
new { controller = "controller", action = "action", sessionId = 0 });
Then in your controller:
public ActionResult Action(int sessionId)
{
}
In your controller you still have direct access to the Request object, so you can use Request.Url, etc.
Does that answer your question, or is it something else that you need?