I'm trying to implement localization in my ASP.NET MVC application using routing.
For example:
www.example.com/Home/Index - will show content for default culture.
www.example.com/en/Home/Index - will show english content.
www.example.com/ru/Home/Index - russian and so on...
So, I've created Localization Attribute for each action:
public class LocalizationAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.RouteData.Values["lang"] != null && !String.IsNullOrWhiteSpace(filterContext.RouteData.Values["lang"].ToString()))
{
var lang = filterContext.RouteData.Values["lang"].ToString();
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(lang);
}
else
{
var langHeader = String.Empty;
langHeader = filterContext.HttpContext.Request.UserLanguages[0];
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(langHeader);
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(langHeader);
}
base.OnActionExecuting(filterContext);
}
}
I'm adding that attribute to every action in my controllers.
And registering routes:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Localization",
"{lang}/{controller}/{action}/{id}",
new { lang = "en-US", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Navigation to www.example.com/About/Contacts works fine.
But when I navigate to www.example.com/en/About/Contacts or something like that, I get 404 error: resource not found.
So, what could be the problem?
Thanks in advance for your help!
By navigating to www.example.com/en/About/Contacts you are actually still invoking the default route. In this case both routes will match but only the last specified one (top to
bottom) will be invoked.
So when you navigate to that url the default route will assume the following:
Controller: en
Action: About
Id: Contacts
You should add constraints to your routes to make them more specific.
Or you could move the "Localization" route below the "Default" route, that way both will still match but in this case the last one will be "Localization".
If you have to troubleshoot anything similar in the future I suggest you to use the RouteDebugger NuGet package, it will help you understand which routes match a request and which one will actually execute and with which parameters.. Here's the link to Phil Haack blog entry about it: http://haacked.com/archive/2011/04/12/routedebugger-2.aspx
Related
I am using the standard MVC template in VS 2013.
With the default set up, http://website/ will be routed to website/Home/Index.
How do I route all "actions" directly under website root url, eg http://website/xxx, to show the same content as http://website/Home/xxx? For example, how do I make http://website/About to execute the About action in the Home controller? If possible, the solution shouldn't be a Http redirect to http://website/Home/About because I don't want to show the "ugly" Home/ in the url.
I couldn't find an answer to this that covered all issues one would face with a public facing website without being a pain to upkeep while still maintaining flexibility.
I ended up coming up with the following. It allows for use of multiple controllers, doesn't require any upkeep, and makes all URLs lowercase.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
//set up constraints automatically using reflection - shouldn't be an issue as it only runs on appstart
var homeConstraints = #"^(?:" + string.Join("|", (typeof(Controllers.HomeController)).GetMethods(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.DeclaredOnly).Select(x => (x.Name == "Index" ? "" : x.Name))) + #")$";
//makes all urls lowercase, which is preferable for a public facing website
routes.LowercaseUrls = true;
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//maps routes with a single action to only those methods specified within the home controller thanks to contraints
routes.MapRoute(
"HomeController",
"{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { action = homeConstraints }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
you can try out like the following one
routes.MapRoute(
name: "MyAppHome",
url: "{action}/{wa}",
defaults: new { controller = "Home", action = "Index", wa = UrlParameter.Optional, area = "Admin" },
namespaces: new string[] { "MyApp.Controllers" }
).DataTokens = new RouteValueDictionary(new { area = "Admin" });
Here, you may notice that the Home controller is hardcoded and is no longer to be supplied in the request. you can also make use of the RouteDebugger to play with routes.
HTH
I want to reserve the root of my website to be for standard webforms and have the MVC pages in a subdirectory Views so I have the following..
routes.MapRoute(
"Default", // Route name
"Views/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
However, even though this works and I see a few work-arounds, I'm not quite happy that the RedirectToAction seems to direct me to the wrong page e.g.
return RedirectToAction("Index", "Home");
Takes me to http://localhost/Views which gives me a resource not found and the Index action on the HomeController doesn't fire. Is there a better way of implementing what I want here or am I missing something obvious?
As you know Views is kind of a reserved name in ASP.NET MVC. It's an existing directory. You could set the RouteExistingFiles to true in your route definitions:
public static void RegisterRoutes(RouteCollection routes)
{
routes.RouteExistingFiles = true;
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"views/{controller}/{action}/{id}",
new { controller = "home", action = "index", id = UrlParameter.Optional }
);
}
Now when you navigate to http://example.com/views or http://example.com/views/homeor http://example.com/views/home/index it will be the Index action of Home controller that will get executed.
I have the following route:
routes.MapRoute(
"Property",
"{language}/property/{propertyUrlId}",
new { controller = "PropertyDetails", action = "Property" }
This is the Controller that should be called for that route:
public class PropertyDetailsController : Controller
{
public ActionResult Property(string language, string propertyUrlId)
{
etc.
And the following URL that should use that route:
http://domain.com/en-us/property/3
Instead, I get 404. Any ideas why?
Here are my routes:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"Property",
"property/{propertyUrlId}",
//new { controller = "PropertyDetails", action = "Property" }, new { language = #"[a-zA-Z]{2}-[a-zA-Z]{2}" }
new { controller = "PropertyDetails", action = "Property" }
);
}
Didn't work with language, or with language/country, either.
You most likely have registered the default route before your Property route. Default route typically looks like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Just register your Property route BEFORE this default route and it will work.
Why it fails? (Assuming you are indeed registering default route first)
en-us -> is interpreted as controller
property -> is interpreted as action
Since you don't have a en-usController with a Property action -> 404
Use "en-us" as a segment of the URL is completely fine. I guess you have registered other routes as well. Try to bring this route to the top of others and at least on top of the default route.
I have tested the scenario, it works just fine for me.
Considering that you want to have the structure of the url as:
http://domain.com/en-us/property/3
use this routing:
routes.MapRoute(
"Property", // Route name
"{language}/property/{propertyUrlId}", // URL with parameters
new { controller = "PropertyDetails", action = "Property", propertyUrlId = UrlParameter.Optional } // Parameter defaults
);
if there is a default routing in your Global.asax file, like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Put the routint above this block of code.
And your Controller Action should look like this:
public ActionResult Property(int propertyUrlId)
{
return View();
}
First of all, there is no reason to break {language} apart into two chunks in the route. As some of you stated, this is fine:
routes.MapRoute(
"Property",
"{language}/property/{propertyUrlId}",
new { controller = "PropertyDetails", action = "Property" }
Second, I omitted some information which was crucial to the solving of this problem. It didn't occur to me to include this in my problem description, as I didn't know there was any relationship. The MVC project is in a solution which also contains a website (non-MVC) which is using the Sitecore CMS as its datastore. Sitecore was stripping out the language segment of the URL and storing it, itself. Once I learned that this was happening, I was able to deal with the problem.
I appreciate all the input, and I apologize for the confusion.
I am in the process of debugging a routing issue on my MVC 3 application and I am using Phil Hacks routing debugger.
I cannot seem to work out where the route highlighted in yellow below is originating. Each time I run my application with the following request
http://www.mywebsite.com/auth/login?ReturnUrl=/
this route comes first and then gives me a 404 error as I do not have an index action. As you can see I have set my default routes to use the Login action method but still this route persists.
I have the following route configurations:
AuthAreaRegistration
public class AuthAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Auth";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"login",
"auth/login/{*returnPath}",
new { controller = "Auth", action = "LogIn", id = UrlParameter.Optional }
);
context.MapRoute(
"Auth_default",
"Auth/{controller}/{action}/{id}",
new { controller = "Auth", action = "LogIn", id = "" }
);
}
}
Global.asax (Using T4 MVC Templates)
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Home",
"{controller}/{action}/{id}",
MVC.Home.Index(), new { id = UrlParameter.Optional },
new string[] { "MyNamespace.WebUI.Controllers" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
MVC.Home.Index(), new { id = UrlParameter.Optional },
new string[] { "MyNamespace.WebUI.Controllers" }
);
}
I don’t like to answer my own question but after a day of trying to solve this problem I thought I would post the answer in case anyone else has the same issue.
It turns out that my application was holding onto old routes and populating them into my route collection. I deleted all the files in my bin folder and rebuilt my solution and everything worked as it should.
I have answered this question in a little more detail here:
Does ASP.NET MVC create default routes for areas
I think the problem is in the fact that you have an area called Auth and a controller called Auth outside of the areas.
MVC will try to match your url against Auth area first but you actually want it to hit your auth controller outside an area.
The best way to solve it imho is to avoid a ambiguous names of controllers/areas.
I get a 404 error when I navigate to the following URL using the route below:
http://localhost:53999/properties/
However, all the following are correctly routed to the List action in my controller:
http://localhost:53999/properties/usa/new-york/manhattan/12
http://localhost:53999/properties/usa/new-york/manhattan
http://localhost:53999/properties/usa/new-york
http://localhost:53999/properties/usa
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//properties
routes.MapRoute(
"Properties",
"Properties/{country}/{state}/{city}/{id}",
new
{
controller = "Properties",
action = "List",
country = UrlParameter.Optional,
state = UrlParameter.Optional,
city = UrlParameter.Optional,
id = UrlParameter.Optional
}
);
//default
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
In PropertiesController.cs:
public ActionResult List(string country, string state, string city, string id)
{
return View();
}
Anyone know what I'm missing? Looks like it should just go to the default action, but it obviously doesn't...
You can also try the following (since you're using MVC 1.0).
Add a route above the current route:
routes.MapRoute(
"Properties", "Properties", new { controller = "Properties", action = "List"}
);
And add an overloaded ActionResult List() method to your controller:
public ActionResult List()
{
return View();
}
Have you tried this Route Debugger from Phil Haack? It may help you determine what is going on.
Instead of passing
(string)null
try passing
UrlParameter.Optional
as specified in Phil Haacks post here. I don't know if this will solve the issue as I'm not currently in a position to test it.
Brad Wilson answered it on this post: http://forums.asp.net/p/1527697/3690295.aspx#3690295
"No, the problem is that you have a Properties folder on your disk, so it's dispatching through the standard dispatcher (not MVC), and then 404ing because it can't find a default document."