Can .net mvc routing cause JavaScript errors? - asp.net-mvc

I am having a lot of trouble using routing infrastructure of asp.net mvc2. I have following routes registered in my global.asax file
routes.MapRoute(
"strict",
"{controller}.mvc/{docid}/{action}/{id}",
new { action = "Index", id = "", docid = "" },
new { docid = #"\d+"}
);
routes.MapRoute(
"default",
"{controller}.mvc/{action}/{id}",
new { action = "Index", id = "" },
new { docConstraint = new DocumentConstraint() }
);
The problem is with first route ("strict"). Three kind of urls can match first route. mycontroller/23/myaction, mycontroller/23/myaction/12 or mycontroller/23/mvaction/stringid. If I try to use this route without specifying value of id everything works fine for example:
Html.ActionLink("Link text", "ActionName", new{docid = 23});
Everything goes well, but if I use links like:
Html.ActionLink("Link text", "ActionName", new{docid = 23, id = 223})
This will produce url currentcontroller.mvc/23/ActionName/223 that is absolutely correct but when it loads the page it gives a JavaScript error in jquery1.4.2.min.js file.
This is strange: if I change id to someid =223 it will reflect in query string and there will be no JS error.
Edit: I have done some further debugging and found when both id and docid are mentioned in route values one thing is ignored in global.asax that is the ignore path.
routes.RouteExistingFiles = false;
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.ignoreRoute is totally bypassed and I can see names of JS files in route value dictionary while debugging in my controller.

it gives javascript error in
jquery1.4.2.min.js file
The most likely cause for this is that something you are displaying on the page is different and you are performing an action that is causing the error. Can you supply enough of a sample from the rendered page to show what you are using jQuery for?

If we drag scripts from solution explorer to site.master it results in following output
<script type="text/javscript" src="../../scripts/jquery.min.js"></script>
The leading dots (..) are creating the problem. Putting source path in url.content or using /scripts instead of ../../scripts will solve the problem because these leading periods are forcing them to match some route in global.asax.

Related

Asp.Net MVC localizing routes with default language

I need to provide language specific routes for my Asp.Net MVC application. The language should be part of the Url Path (http://myapp/en/Blog) and when it is ommitted the default language have to be used.
http://myapp/en/Blog -> Blog in the English version
http://myapp/Blog -> Blog in the Default (portuguese) Language version
To address this issue I created two Routes bellow:
routes.MapRoute(
name: "Default.lang",
url: "{lang}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { lang = #"^[a-zA-Z]{2}$" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The routes are working as expected but I'm getting weird results when I try to use the Url.RouteUrl method to get alternative language urls.
Example 1 - Path: /
url.Action("Index", "Blog") // returns "/Blog" that is OK
url.Action("Index", "Blog", new { lang = "en" }); // returns "/en/Blog" that is also OK
Example 2 - Path: /en
url.Action("Index", "Home") // returns "/en/Blog" (??????????) Not OK
url.Action("Index", "Home", new { lang = "en" }); // returns "/en/Blog" that is OK
As you can see I get a wrong result when I access the url http://myappurl/en and try to use the Url.Action method without pass any route value (same result with Url.RouteUrl)
Does anyone knows what is wrong with my routes?
[EDIT] I'm not sure if the issue is related to the route because I've tested the routes using "en" as first route's constraint and I got the same result.
After some digging inside System.Web.Mvc and System.Web.Routing source code I found that this behavior is expected. I presume that it is designed to correctly work in applications running inside Virtual Directories.
This can be confirmed in this answer that I found when was researching if someone else had the same problem with virtual path and route resolution.
Workaround
Use named route resolution with Url.RouteUrl method that has and different implementation and works as expected.
Example:
var blogDefaultUrl = url.RouteUrl("Default", new {action = "Index", controller = "Blog"});
var blogLangageSpecifictUrl = url.RouteUrl("Default.lang", new { action = "Index", controller = "Blog", lang = language });
I was avoiding to use named routes because my application design is a little bit more complicated than I demonstrated above. By this reason I have to discover at run time the route that matches the Request.Url and from that point I call the RouteUrl to get the alternative languages url to the content.

MVC Routing Redirection

I have a webservice project that is being expanded to host multiple APIs instead of just one, so I wanted to cleanup the routes without breaking the old version. The main API used to sit off of a controller named API and accept parameters like this:
api/{language}/{action}/{*parameters}
Now, I have an Area named API that is going to house each of the APIs in their own controller and the route will look like this:
api/{controller}/{language}/{action}
I still need the old route to be usable for legacy apps already using the old route, I was hoping just to be able to create a 2nd "legacy" route that would catch the old pathing and use the new controller. I tried this but it only worked then with the new location and the ikd one returned a 404.
api/{language}/{action}/{*parameters}",
new { controller = "api1", action = "Index", language = "json" }
Any ideas on how to accomplish this? I tried RouteMagic but it didn't seem to work. Maybe I set the redirect up wrong though.
Ok, figured out after getting a sandwhich...just needed to add route constraints so that the old version would not catch on the new route format:
routes.MapRoute(
"API_default",
"api/{controller}/{language}/{action}",
new { controller = "api1", action = "Index", language = "json" },
new { language = "json|xml" }
);
routes.MapRoute(
"API_legacy",
"api/{language}/{action}/{*parameters}",
new { controller = "api1", action = "Index", language = "json", parameters = UrlParameter.Optional },
new { language = "json|xml" }
);

MVC - Application root appears twice in url using Url.Content/Url.Action

I have several mvc applications on the same domain, each have their own directory.
mydomain.com/app1
mydomain.com/app2
etc..
When using Url.Content() and Url.Action() when at the root level, 'app1' part is repeated twice in the urls.
// code used to generate the links
<%= Url.Action("tetris", "Apps") %>
Page Url: mydomain.com/app1/
rendered link (broken): mydomain.com/app1/app1/Apps.aspx/tetris
application root appears twice in the rendered url when at the root directory
Page Url: mydomain.com/app1/home.aspx/
rendered link (works): mydomain.com/app1/Apps.aspx/tetris
application root appears once - everything works as expected
my routes - I'm using the routes from Phil haacks blog post
routes.MapRoute(
"Default",
"{controller}.aspx/{action}/{id}",
new { action = "Index", id = "" }
);
routes.MapRoute(
"Root",
"",
new { controller = "Home", action = "Index", id = "" }
);
Any ideas?
That is because applications process web.configs in order of inner most director to outer most directory.
You have two options to fix this behavior.
Specify the namespaces you want the routes to apply to either in the root directory or your sub-directories. http://msdn.microsoft.com/en-us/library/dd504958.aspx
routes.MapRoute(
"Root", "", new {...},
new[] { "MyNamespace.For.Root" }
);
Or in the root directory specify your sub-directories as ignored routes using. http://msdn.microsoft.com/en-us/library/dd505203.aspx
routes.IgnoreRoute("/app1");
routes.IgnoreRoute("/app2");
When I had this issue it was because someone used RouteArea["app1"] attribute on the Controller but also included it on the action GET["apps1/Detail"] instead of just GET["Detail"]

ASP.NET MVC 2 One Route Works, One Route Doesn't

I have two routes in my ASP.NET MVC application.
The first is working fine - it's an ActionResult that returns a view.
The second is on the same controller and is an ActionResult that returns a Json response. It takes a couple of additional paramaters.
This second route is working on my dev machine, but when I deploy it to the server I get back a blank response. Any suggestions will be gratefully received.
I have also copy-pasted the route into a browser to eliminate any issues in the jQuery JavaScript.
The method
[HttpGet]
public ActionResult CheckSku(string id, string brand) {
CheckSkuModel model = new CheckSkuModel();
model.Id = id;
model.Brand = brand;
return Json(model, JsonRequestBehavior.AllowGet);
}
The routes
routes.MapRoute(
"Default", // Route name
"{controller}.mvc/{action}/{id}", // URL with parameters
new {
controller = "Orders", action = "Send", id = ""
} // Parameter defaults
);
routes.MapRoute(
"CheckSku", // Route name
"{controller}.mvc/{action}/{id}/{brand}", // URL with parameters
new {
controller = "Orders", action = "CheckSku", id = "", brand = ""
} // Parameter defaults
);
Two thing you can quickly check that may help:
1. Swap the two routes around so "CheckSku" is above "default"
2. Make the "CheckSku" more specific so will look something like:
routes.MapRoute(
"CheckSku", // Route name
"Orders.mvc/CheckSku/{id}/{brand}", // URL with parameters
new {
controller = "Orders", action = "CheckSku", id = "", brand = ""
} // Parameter defaults
);
that was another controller doesn't pick up the url by mistake.
An alternative is the use the routelink helper when generating the url so it points to the correct route.
When you say you "get back a blank response", do you mean you're not getting an error, but that the server isn't returning anything? What makes you think this is a routing issue?
Some troubleshooting tips:
Use Fiddler to examine the HTTP request you're making and the raw HTTP response the server is sending back. If you're getting a 500 response code then check the response for error information.
Can you attach a remote debugger to the server? This might give you a chance to investigate any exceptions that are raised.

Asp.Net MVC Url.RouteUrl Problem on Windows 2003 vs. Visual Studio 2008

I'm seeing a difference in the output from Url.RouteUrl between my development machine and my deployment server. I'm running Visual Studio 2008 and my deployment box is Windows 2003 Server. I have configured the Global.asax.cs to run with the .aspx extension in my routing tables. However, when I use the "Search-Basic" named route, there is no output from Url.RouteUrl("Search-Basic", new {category = "Test", searchExpression = "search this"})
View Code:
<%= Url.RouteUrl("Search-Basic", new {category = "test", searchExpression="search this"}) %>
Global.asax.cs Code:
// routes for IIS 6 and version below
routes.MapRoute(
"Search-Basic",
"Search.aspx/Basic/{category}",
new { controller = "Search", action = "Basic", category = "All" }
);
routes.MapRoute(
"Default", // Route name
"{controller}.aspx/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
routes.MapRoute(
"Root",
"",
new { controller = "Home", action = "Index", id = "" }
);
On my development box, I get the expected output: /Search.aspx/Basic/Test?searchExpression=search%20this
However, on my deployment server I get no output at all. One difference perhaps is that I'm running the application in a virtual directory on my deployment server; something like: http://testmachine.com/sm/testappname/ where "/sm" is a virtual directory and "/testappname" is a virtual directory holding my application.
Any ideas?
Thank you kindly.
Are you running the same version of ASP.NET MVC because there is a bug in the RC1 (non-refresh) that causes this behavious when you have a route where e.g. the controller is not specified in the route:
The other regression is that in some
cases, the RouteUrl (and thus
RouteLink) methods return an empty
string when you specify a route name,
but the route has default parameters
which are not parameters in the URL.
For example, if you have the following
route:
routes.MapRoute("route-name", "foo/bar", new {controller="Home", action="index"});
Notice that controller has default
value, but is not part of the URL. If
you then specify:
<%= Url.RouteUrl("route-name") %>
You might expect that it would use
that route to render the URL, but it
doesn’t. This bug was introduced when
we refactored all our url generating
helpers to call into a common method.
It turns out, however, that our
RouteUrl methods (aka non-MVC
specific) should have subtly different
behavior than the MVC specific methods
(such as Action). We added a flag to
the common method so that this
difference is taken into
consideration. This was a fix that did
not have a large surface area.
See http://haacked.com/archive/2009/01/30/aspnetmvc-refresh.aspx

Resources